c# 复杂和大型 xml 加载

c# complex and large xml loading

我在用 c# 解析巨大的 xml 时遇到一些问题,主要是因为我在很长一段时间后从 apex 返回到 c#。到目前为止我连这个都无法工作

 private void read_Click(object sender, EventArgs e)
    {
        XElement xmlDoc = XElement.Load(@"D:\AOI\Samples\Error\60A84130868D_20180428035150_AOI-mek1.xml");
        var loaded_File =
                from fileInfo in xmlDoc.Descendants("result_file")

                select new File
                {

                    filename = fileInfo.Element("designator").Value,
                    supplier = fileInfo.Element("supplier").Value,
                    date_created = fileInfo.Element("date").Value,
                    station_ID = fileInfo.Element("station_ID").Value,
                    operator_ID = fileInfo.Element("operator_ID").Value,
                    program = fileInfo.Element("program").Value,
                    side_variant = fileInfo.Element("side_variant").Value
                };
        foreach(var item in loaded_File) { 
            System.Diagnostics.Debug.WriteLine(item.ToString());
        }

    }

Xml 文件看起来如下,有多个 good_no 和 error_no,有人可以告诉我如何正确加载文件吗?之后我需要它来将它插入数据库,但这应该没问题。

<result_file>
  <filename>60Axxxxxek1</filename>
  <supplier>Maxxxxz</supplier>
  <date>20xxxx5150</date>
  <station_ID>Axxxx1</station_ID>
  <operator_ID></operator_ID>
  <program>Xxxx01</program>
  <side_variant>A</side_variant>
  <pcbs_in_panel>0</pcbs_in_panel>
  <serial>60xxxxxx8D</serial>
  <status>GOOD</status>
  <starttime>20180xxxxxx150</starttime>
  <lot_no></lot_no>
  <info>
    <window_no>354</window_no>
    <packs_no>343</packs_no>
    <error_total>1</error_total>
    <error_conf>0</error_conf>
    <inspection_time>5</inspection_time>
    <panel_image>AOxxxxx_A.jpg</panel_image>
    <panel_image_location>x:\xml</panel_image_location>
    <ng_image_location>x:\xml\Xxxxx0428</ng_image_location>
    <repaired>0</repaired>
  </info>
  <errors>
    <error_no name="1">
      <designator></designator>
      <pin></pin>
      <stamp_name>Bridge:Short</stamp_name>
      <package_name></package_name>
      <errortype>-</errortype>
      <error_contents></error_contents>
      <pcb_no></pcb_no>
      <feeder_no></feeder_no>
      <pos_x>8760</pos_x>
      <pos_y>4600</pos_y>
      <window>-313</window>
      <ng_message></ng_message>
      <comment>(* *){Bridge:Short}</comment>
      <ng_image>Xxxxxx13.jpg</ng_image>
    </error_no>
  </errors>
  <goods>
    <good_no name="1">
      <designator>Ixxx1</designator>
      <pin>Ixxx1</pin>
      <stamp_name>Ixxxxrat</stamp_name>
      <package_name>Ixxxx1</package_name>
      <pcb_no></pcb_no>
      <feeder_no></feeder_no>
      <pos_x>3082</pos_x>
      <pos_y>3202</pos_y>
      <window>+1</window>
      <comment>(* *){Ixxxxat}</comment>
    </good_no>
   </goods>
  </result_file>

感谢您的建议。

编辑: 我也准备了类那个

public class File
    {
        public string name { get; set; }
        public string filename { get; set; }
        public string supplier { get; set; }
        public string date_created { get; set; }
        public string station_ID { get; set; }
        public string operator_ID { get; set; }
        public string program { get; set; }
        public string side_variant { get; set; }
        public string pcbs_in_panel { get; set; }
        public string serial { get; set; }
        public string status { get; set; }
        public string starttime { get; set; }
        public string lot_no { get; set; }
        public string window_no { get; set; }
        public string packs_no { get; set; }
        public string error_total { get; set; }
        public string error_conf { get; set; }
        public string inspection_time { get; set; }
        public string panel_image { get; set; }
        public string panel_image_location { get; set; }
        public string ng_image_location { get; set; }
        public string repaired { get; set; }
        public List<Good> Goods = new List<Good>();
        public List<Error> Errors = new List<Error>();
    }
    public class Good
    {
        public List<Good_no> Good_ones = new List<Good_no>();
    }
    public class Error
    {
        public List<Error_no> Error_ones = new List<Error_no>();
    }
    public class Good_no
    {
        public string name { get; set; }
        public string designator { get; set; }
        public string pin { get; set; }
        public string stamp_name { get; set; }
        public string package_name { get; set; }
        public string pcb_no { get; set; }
        public string feeder_no { get; set; }
        public string pos_x { get; set; }
        public string pos_y { get; set; }
        public string window { get; set; }
        public string comment { get; set; }
    }
    public class Error_no
    {
        public string name { get; set; }
        public string designator { get; set; }
        public string pin { get; set; }
        public string stamp_name { get; set; }
        public string package_name { get; set; }
        public string errortype { get; set; }
        public string error_contents { get; set; }
        public string pcb_no { get; set; }
        public string feeder_no { get; set; }
        public string pos_x { get; set; }
        public string pos_y { get; set; }
        public string window { get; set; }
        public string ng_message { get; set; }
        public string comment { get; set; }
        public string ng_image { get; set; }
    }

如果你想阅读XML-文件,我建议你反序列化XML-文件。 为此,您的 class-定义不完整。在 Visual Studio 中有一个工具,叫做 xsd.exe。使用此工具,您可以首先在 XSD-Schema 中转换 XML-File。从 XSD-Schema 您可以生成所需的 classes.

转换为 XSD-Schema:xsd.exe {Filename.xml}

转换为 C#-类:xsd.exe /c {Filename.xsd}

XSD 有更多选项(参见:https://msdn.microsoft.com/de-de/library/x6c1kb0s(v=vs.120).aspx

如果您有正确的 classes,您可以使用 XMLSerializer 读取 XML-File(参见:https://msdn.microsoft.com/de-de/library/tz8csy73(v=vs.110).aspx

您应该简化 class 结构:

public class File
{
    public string Filename { get; set; }
    public string Supplier { get; set; }
    // ...
    public List<Good> Goods { get; set; }
    public List<Error> Errors { get; set; }
}
public class Good
{
    public string Name { get; set; }
    public string Designator { get; set; }
    public string Pin { get; set; }
    // ...
}
public class Error
{
    public string Name { get; set; }
    public string Designator { get; set; }
    // ...
}

那么读数将如下所示:

var xmlDoc = XElement.Load("test.xml");

var loadedFile = new File
{
    Filename = xmlDoc.Element("filename").Value,
    Supplier = xmlDoc.Element("supplier").Value,
    // ...
    Goods = (from good in xmlDoc.Element("goods").Elements("good_no")
             select new Good
             {
                 Name = good.Attribute("name").Value,
                 Designator = good.Element("designator").Value,
                 Pin = good.Element("pin").Value
                 // ...
             })
             .ToList()
};