c#complex和大xml加载

时间:2018-05-16 08:39:49

标签: c# xml linq xml-parsing linq-to-xml

我在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; }
    }

2 个答案:

答案 0 :(得分:1)

您应该简化类结构:

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()
};

答案 1 :(得分:0)

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

转换为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

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