开发者

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

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

<root>
       <node></node>
       <node></node>开发者_如何学C
</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.


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");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜