[quoted text, click to view] Markus wrote:
[quoted text, click to view] > What i try to say is that if you do not execute the first list.count, your
> nodeLIst will be lost.
Right, as far as I currently understand what we observe is that there
seems to be some lazy evaluation happening, meaning the XmlNodeList
instance returned by SelectNodes is only built once its content is
accessed (as our examples do with outputting the Count property).
So what nodes you find in the XmlNodeList does not depend on the XML
document or node contents when SelectNodes call happens and returns the
XmlNodeList instance but when the Count is accessed first.
I am currently asking elsewhere whether that is a bug or an intended
feature. It looks more like a bug in my understanding, if it is a
feature then it needs proper documentation.
Current test case is
XmlDocument doc = new XmlDocument();
doc.LoadXml("<parent><child><foo>bar</foo></child><child><foo>bar</foo></child><child><foo>not
bar</foo></child></parent>");
XmlNodeList list1 = doc.SelectNodes("parent/child[foo='bar']");
XmlNodeList list2 = doc.SelectNodes("parent/child[foo='bar']");
XmlNodeList list3 = doc.SelectNodes("parent/child[foo='bar']");
Console.WriteLine("Before removeAll:");
Console.WriteLine("list1.Count: {0}.", list1.Count);
doc.DocumentElement.RemoveAll();
Console.WriteLine("After removeAll:");
Console.WriteLine("list1.Count: {0}.", list1.Count);
Console.WriteLine("list2.Count: {0}.", list2.Count);
doc.DocumentElement.InnerXml =
"<child><foo>bar</foo></child><child>Kibo</child>";
Console.WriteLine("After insertion:");
Console.WriteLine("list1.Count: {0}.", list1.Count);
Console.WriteLine("list2.Count: {0}.", list2.Count);
Console.WriteLine("list3.Count: {0}.", list3.Count);
That results then in the output
Before removeAll:
list1.Count: 2.
After removeAll:
list1.Count: 2.
list2.Count: 0.
After insertion:
list1.Count: 2.
list2.Count: 0.
list3.Count: 1.
Thus although all SelectNodes call are identical and are made before the
document is being modified those three lists end up with different Count
property values depending on when Count is being first accessed.
--
Martin Honnen --- MVP XML