如何在不将其加载到内存中并使用 XElement 的情况下读取大型 xml 文件

如何在不将其加载到内存中并使用 XElement 的情况下读取大型 xml 文件

问题描述:

我想读取一个大的 xml 文件(100+M).由于它的大小,我不想使用 XElement 将它加载到内存中.我正在使用 linq-xml 查询来解析和读取它.

I want to read a large xml file (100+M). Due to its size, I do not want to load it in memory using XElement. I am using linq-xml queries to parse and read it.

最好的方法是什么?关于 XPath 或 XmlReader 与 linq-xml/XElement 组合的任何示例?

What's the best way to do it? Any example on combination of XPath or XmlReader with linq-xml/XElement?

请帮忙.谢谢.

是的,您可以将 XmlReader 与 方法 XNode.ReadFrom,参见文档中的示例,该示例使用 C# 将 XmlReader 发现的节点选择性地处理为 XElement.

Yes, you can combine XmlReader with the method XNode.ReadFrom, see the example in the documentation which uses C# to selectively process nodes found by the XmlReader as an XElement.