Parse xml in c# : combine xmlreader and linq to xml

浪子不回头ぞ 提交于 2019-12-09 23:00:37

问题


I have to parse large XML file in C#. I use LINQ-to-XML. I have a structure like

<root>
       <node></node>
       <node></node>
</root>

I would like use XmlReader to loop on each node and use LINQ-to-XML to get each node and work on it ?

So I have only in memory the current node.


回答1:


You can do something like that:

string path = @"E:\tmp\testxml.xml";
using (var reader = XmlReader.Create(path))
{

    bool isOnNode = reader.ReadToDescendant("node");
    while (isOnNode)
    {
        var element = (XElement)XNode.ReadFrom(reader);

        // Use element with Linq to XML
        // ...

        isOnNode = reader.ReadToNextSibling("node");
    }
}


来源:https://stackoverflow.com/questions/7028731/parse-xml-in-c-sharp-combine-xmlreader-and-linq-to-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!