C# xml 读/写/xpath 不使用 XmlDocument
我正在重构现有系统中的一些代码。目标是删除 XmlDocument 的所有实例以减少内存占用。然而,当某些规则适用时,我们使用 XPath 来操作 xml。有没有一种方法可以在不使用将整个文档加载到内存中的类的情况下使用 XPath?我们用 XmlTextReader 替换了所有其他实例,但这些实例之所以有效,是因为没有 XPath 并且读取非常简单。
某些 XPath 使用其他节点的值作为其决策的基础。例如,消息节点的值可能基于金额节点的值,因此需要一次访问多个节点。
I am refactoring some code in an existing system. The goal is to remove all instances of the XmlDocument to reduce the memory footprint. However, we use XPath to manipulate the xml when certain rules apply. Is there a way to use XPath without using a class that loads the entire document into memory? We've replaced all other instances with XmlTextReader, but those only worked because there is no XPath and the reading is very simple.
Some of the XPath uses values of other nodes to base its decision on. For instance, the value of the message node may be based on the value of the amount node, so there is a need to access multiple nodes at one time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的 XPATH 表达式基于访问多个节点,您只需将 XML 读入 DOM 即可。不过有两件事。首先,您不必将其全部读入 DOM,只需将您正在查询的部分读入即可。其次,您使用的 DOM 会有所不同; XPathDocument 是只读的,并针对 XPATH 查询速度进行了调整,这与更通用但昂贵的 XmlDocument 不同。
If your XPATH expression is based on accessing multiple nodes, you're just going to have to read the XML into a DOM. Two things, though. First, you don't have to read all of it into a DOM, just the part you're querying. Second, which DOM you use makes a difference; XPathDocument is read-only and tuned for XPATH query speed, unlike the more general purpose but expensive XmlDocument.
我认为使用 System.Xml.Linq.XDocument< /a> 也被禁止吗?否则,它将是一个不错的选择,因为它比 XmlDocument 更快(据我记得)。
I supose that using System.Xml.Linq.XDocument is also prohibited? Otherwise, it would be a good choice, as it is faster than XmlDocument (as I remember).
支持 XPath 意味着支持如下查询:
或
需要 XPath 处理器能够查看文档中的任何位置。您不会找到只进的 XPath 处理器。
Supporting XPath means supporting queries like:
or
which require the XPath processor to be able to look everywhere in the document. You're not going to find a forward-only XPath processor.
执行此操作的方法是使用 XPathDocument,它可以采用流 - 因此您可以使用 StringReader。
这会以正向读取的方式返回值,而无需使用 XmlDocument 将整个 XML DOM 加载到内存中。
下面是一个返回满足 XPath 查询的第一个节点的值的示例:
The way to do this is to use XPathDocument, which can take a stream - therefore you can use StringReader.
This returns the value in a forward read way without the overhead of loading the whole XML DOM into memory with XmlDocument.
Here is an example which returns the value of the first node that satisfies the XPath query: