在大型xml文件C#中使用XMLreader和xpath

所以我有这个相当大的XML文件,我需要解析,我不想将整个文件加载到内存中。 XML看起来像这样:

          

我想要做的是遍历每个名​​为node的节点,看看该属性是否与我的search-critera相匹配。 我想用xpath做到这一点。 我在c#中找到了Parse xml:将xmlreader和linq结合到xml中 ,这有助于我隔离有问题的节点。 但我不能在父节点上使用xpath。 我想我必须创建一个xmldocument并加载读者,但我不能按照我想要的方式工作。

属性需要围绕值的双引号(childattrib)。 尝试以下是xml阅读器和xml linq的组合。 读取大型xml文件时,请始终使用xmlreader。

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace ConsoleApplication74 { class Program { const string FILENAME = @"c:\temp\test.xml"; static void Main(string[] args) { XmlReader reader = XmlReader.Create(FILENAME); while (!reader.EOF) { if (reader.Name != "node") { reader.ReadToFollowing("node"); } if (!reader.EOF) { XElement node = (XElement)XElement.ReadFrom(reader); if ((Boolean)node.Attribute("attrib")) { } } } } } }