C# xml 读/写/xpath 不使用 XmlDocument

发布于 2024-08-06 13:20:20 字数 252 浏览 2 评论 0原文

我正在重构现有系统中的一些代码。目标是删除 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 技术交流群。

扫码二维码加入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.

超可爱的懒熊 2024-08-13 13:20:20

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).

恍梦境° 2024-08-13 13:20:20

支持 XPath 意味着支持如下查询:

//address[/states/state[@code=current()/@code]='California']

//item[@id != preceding-sibling/item/@id]

需要 XPath 处理器能够查看文档中的任何位置。您不会找到只进的 XPath 处理器。

Supporting XPath means supporting queries like:

//address[/states/state[@code=current()/@code]='California']

or

//item[@id != preceding-sibling/item/@id]

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.

向地狱狂奔 2024-08-13 13:20:20

执行此操作的方法是使用 XPathDocument,它可以采用流 - 因此您可以使用 StringReader。

这会以正向读取的方式返回值,而无需使用 XmlDocument 将整个 XML DOM 加载到内存中。

下面是一个返回满足 XPath 查询的第一个节点的值的示例:

public string extract(string input_xml)
    {
        XPathDocument document = new XPathDocument(new StringReader(input_xml));
        XPathNavigator navigator = document.CreateNavigator();
        XPathNodeIterator node_iterator = navigator.Select(SEARCH_EXPRESSION);
        node_iterator.MoveNext();
        return node_iterator.Current.Value;
    }

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:

public string extract(string input_xml)
    {
        XPathDocument document = new XPathDocument(new StringReader(input_xml));
        XPathNavigator navigator = document.CreateNavigator();
        XPathNodeIterator node_iterator = navigator.Select(SEARCH_EXPRESSION);
        node_iterator.MoveNext();
        return node_iterator.Current.Value;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文