使用的XMLReader读取大型XML文档解析信息到一个类 [英] Using XMLReader to read large XML documents to parse the information into a class

查看:104
本文介绍了使用的XMLReader读取大型XML文档解析信息到一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用的XDocument使用LINQ结合,XML在XML文件来加载和填充我的课。

I have been using XDocument combined with LINQ to XML to load in xml files and populate my class.

但现在我与确保我的程序能够处理XML文档的所有尺寸,这意味着我需要使用XML阅读器,并在这个时候是我不能让我的头任务。周围操纵的XMLReader来填充我的课

But now I am tasked with making sure my program can handle all sizes of XML documents which means i need to use XML Reader and at this time being i cant get my head around manipulating the XMLReader to populate my class.

目前,我有下面的类来填充:

currently i have the below class to populate:

 public class DataRecord
  {
    private List<Fields> field = new List<Fields>();

    public string ID { get; set; }
    public string TotalLength { get; set; }

    public List<Fields> MyProperty
    {
      get { return field; }
      set { field = value; }
    }

  }

  internal class Fields
  {
    public string name { get; set; }
    public string startByte { get; set; }
    public string length { get; set; }
  }

}



我一直在尝试切换语句来执行的XMLReader从我提供数据来填充的类。例如:

I have been trying to switch statements to enforce the xmlreader to provide the data from me to populate the class. For example:

using (XmlReader reader = XmlReader.Create(filename))
  {
    while (reader.Read())
    {
      switch (reader.NodeType)
      {
        case XmlNodeType.Element:
          switch (reader.Name)
          {
            case "DataRecord":
              var dataaa = new dataclass.DataRecord();
              break;
          }
         break;
      }
    }
  }



但正如我所说,这是例如,我已经搜索了好半天,试图找到一个答案,但我越来越糊涂。希望有人能帮助我们我的问题。

But as i said this is an example, I have searched for ages to try and find an answer but I am getting confused. Hopefully someone can help we my problem.

推荐答案

您可以使用的XmlReader 在文档中移动,但是如果使用装载每个元素的XElement

You can use XmlReader to move through the document, but then load each element using XElement.

下面是一个简单的例子:

Here's a short example:

using System;
using System.Xml;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        using (var reader = XmlReader.Create("test.xml"))
        {
            while (reader.ReadToFollowing("foo"))
            {
                XElement element = XElement.Load(reader.ReadSubtree());
                Console.WriteLine("Title: {0}", element.Attribute("title").Value);
            }
        }
    }
}

通过示例XML:

<data>
  <foo title="x" /><foo title="y">asd</foo> <foo title="z" />
</data>



(略不一致只是为了显示它能够处理与内容元素,元素之间没有空格,和元素,它们之间的空间。)

(Slightly inconsistent just to show that it can handle elements with content, elements with no space between them, and elements with space between them.)

然后你会做循环显然无论你需要用的XElement - 如果你已经有了建立从您的类的实例的方法的XElement ,你可以调用,使用对象,而你走

Then obviously in the loop you'd do whatever you need to with the XElement - if you've already got a way of creating an instance of your class from an XElement, you can just call that, use the object, and you're away.

这篇关于使用的XMLReader读取大型XML文档解析信息到一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆