c#复杂而庞大的xml加载

c#复杂而庞大的xml加载,c#,xml,linq,xml-parsing,linq-to-xml,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_

我在用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文件如下所示,有多个好的\u no和错误\u 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>

60AXXXEK1
Maxxxxz
20XX5150
Axxxx1
Xxxx01
A.
0
60xxxxxx8D
好的
20180xxxxxx150
354
343
1.
0
5.
AOxxxxx_A.jpg
x:\xml
x:\xml\Xxxxx0428
0
桥:短
-
8760
4600
-313
(**){Bridge:Short}
Xxxxxx13.jpg
Ixxx1
Ixxx1
Ixxxxrat
Ixxxx1
3082
3202
+1
(**){Ixxxxat}
谢谢你的建议

编辑: 我也为此准备了课程

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; }
    }
公共类文件
{
公共字符串名称{get;set;}
公共字符串文件名{get;set;}
公共字符串提供程序{get;set;}
公共字符串日期_创建{get;set;}
公共字符串station_ID{get;set;}
公共字符串运算符_ID{get;set;}
公共字符串程序{get;set;}
公共字符串侧_变量{get;set;}
公共字符串PCB_在_面板{get;set;}
公共字符串序列{get;set;}
公共字符串状态{get;set;}
公共字符串开始时间{get;set;}
公共字符串lot_no{get;set;}
公共字符串窗口{get;set;}
公共字符串包\u no{get;set;}
公共字符串错误\u total{get;set;}
公共字符串错误\u conf{get;set;}
公共字符串检查\u时间{get;set;}
公共字符串面板_图像{get;set;}
公共字符串面板\u图像\u位置{get;set;}
公共字符串ng_image_位置{get;set;}
公共字符串{get;set;}
公共物品清单=新清单();
公共列表错误=新列表();
}
公共物品
{
公共列表好的=新列表();
}
公共类错误
{
公共列表错误_ones=新列表();
}
公共课好吗
{
公共字符串名称{get;set;}
公共字符串指示符{get;set;}
公共字符串pin{get;set;}
公共字符串stamp_name{get;set;}
公共字符串包_name{get;set;}
公共字符串pcb_no{get;set;}
公共字符串feeder_no{get;set;}
公共字符串pos_x{get;set;}
公共字符串pos_y{get;set;}
公共字符串窗口{get;set;}
公共字符串注释{get;set;}
}
公共类错误号
{
公共字符串名称{get;set;}
公共字符串指示符{get;set;}
公共字符串pin{get;set;}
公共字符串stamp_name{get;set;}
公共字符串包_name{get;set;}
公共字符串错误类型{get;set;}
公共字符串错误\u内容{get;set;}
公共字符串pcb_no{get;set;}
公共字符串feeder_no{get;set;}
公共字符串pos_x{get;set;}
公共字符串pos_y{get;set;}
公共字符串窗口{get;set;}
公共字符串ng_消息{get;set;}
公共字符串注释{get;set;}
公共字符串ng_图像{get;set;}
}

如果您想读取XML文件,我建议您反序列化XML文件。 因此,您的类定义不完整。VisualStudio中有一个名为xsd.exe的工具。使用此工具,您可以首先在XSD模式中转换XML文件。从XSD模式可以生成所需的类

要转换为XSD架构,请执行以下操作:XSD.exe{Filename.xml}

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

XSD还有更多选项(请参阅:)


如果您有正确的类,可以使用XMLSerializer读取XML文件(请参阅:)

您应该简化类结构:

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; }
    // ...
}

我无法将其写入debug,foreach不工作,就像它的null一样,毫无例外。所以我不知道它是否加载了。简单地说,我不能收回它。如果你能提供一个我们可以复制并粘贴到控制台应用程序中重新处理问题的程序,那将是非常棒的。缺少什么?我确实提供了当前唯一的代码,包括所有类和使用的xml示例。唯一缺少的信息是它是表单应用程序,所有代码都发生在按钮单击中。只需单击按钮,就不会出现错误,但foreach到debug不是这样工作的(当item.tostring被普通Strings替换时工作)。它是真正的xml吗?它有名称空间吗?不要告诉我们文件是“巨大的”。如果大小是相关的,那么我们需要数字。对于某些人来说是“巨大的”意味着1Mb,对其他人来说意味着1Tb——这是一个很大的区别。
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()
};