慢的SelectSingleNodeSelectSingleNode

2023-09-06 14:52:12 作者:明月清风

我有这样一个简单的结构化的XML文件:

I have a simple structured XML file like this:

<ttest ID="ttest00001", NickName="map00001"/>
<ttest ID="ttest00002", NickName="map00002"/>
<ttest ID="ttest00003", NickName="map00003"/>
<ttest ID="ttest00004", NickName="map00004"/>

..... 该XML文件可以是2.5MB左右。

..... This xml file can be around 2.5MB.

在我的源$ C ​​$ C,我将有一个循环来获得昵称

In my source code I will have a loop to get nicknames

在每个循环中,我有这样的事情:

In each loop, I have something like this:

nickNameLoopNum = MyXmlDoc.SelectSingleNode("//ttest[@ID=' + testloopNum + "']").Attributes["NickName"].Value

这一行将会花费我30到40毫秒。

This single line will cost me 30 to 40 millisecond.

我搜索一些老的文章(可追溯至2002年)说,使用某种编的XPath可以帮助的情况下,但那是5年前。我不知道有没有近代史的做法,使其更快? (我使用.NET 3.5)

I searched some old articles (dated back to 2002) saying, use some sort of compiled "xpath" can help the situation, but that was 5 years ago. I wonder is there a mordern practice to make it faster? (I'm using .NET 3.5)

推荐答案

使用 // 的缩写,在XPath EX pression结果大低效,因为它会导致搜索整个XML文档。使用 // 反复乘以这种低效率。

Using the "//" abbreviation in an XPath expression results in big inefficiency as it causes the whole XML document to be searched. Using '//' repeatedly multiplies this inefficiency.

一个有效的解决方案的问题是通过评估只是一个单一的XPath EX pression获得所有昵称属性节点:

One efficient solution to the problem is to obtain all "NickName" attribute nodes by evaluating just one single XPath expression:

&NBSP;&NBSP;&NBSP; t检验/ @昵称

ttest/@NickName

在上下文节点的父所有的 t检验元素。

where the context node is the parent of all "ttest" elements.

的C#code 将如下所示:

    int n = 15;
    XmlDocument doc = new XmlDocument();
    doc.Load("MyFile.xml");

    XmlNodeList nodeList;
    XmlNode top = doc.DocumentElement;
    nodeList =
        top.SelectNodes("ttest/@NickName");

    // Get the N-th NickName, can be done in a loop for
    // all n in a range

    string nickName = nodeList[n].Value;

在这里,我们假设TTEST元素为XML文档的顶级元素的子元素。

Here we suppose that the "ttest" elements are children of the top element of the xml document.

总结,一个有效的解决方案是presented,其计算XPath EX pression只有一次和地点便利的IEnumerable对象中的所有结果(也可以作为一个数组)来访问所有需要的物品在 0(C)的时间。

To summarize, an efficient solution is presented, which evaluates an XPath expression only once and places all results in a convenient IEnumerable object (that can be used as an array) to access any required item in O(c) time.