473,222 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,222 software developers and data experts.

XPath XmlNodeList documentation - apparent contradition

1. Under the topic "Select Nodes Using XPath Navigation" it says:
"All XmlNodeList objects are synchronized with the underlying document,
therefore if you ... modify the value of a node, that node is updated in the
document it came from."

2. Under the topic "XmlNode.SelectNodes Method (String)" it says:
"The XmlNodeList should not be expected to be connected "live" to the XML
document. That is, changes that appear in the XML diocument may not appear
in the XmlNodeList, and vice versa."

Is this really a contradiction? If so, which one is correct? If not, what
is it that I don't understand here?

TIA
May 22 '06 #1
4 4580


SkyHook wrote:
1. Under the topic "Select Nodes Using XPath Navigation" it says:
"All XmlNodeList objects are synchronized with the underlying document,
therefore if you ... modify the value of a node, that node is updated in the
document it came from."

2. Under the topic "XmlNode.SelectNodes Method (String)" it says:
"The XmlNodeList should not be expected to be connected "live" to the XML
document. That is, changes that appear in the XML diocument may not appear
in the XmlNodeList, and vice versa."


You are right, the documentation is misleading. With .NET some
XmlNodeLists returned are "live", e.g. ChildNodes is live, try

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList documentChildren = xmlDocument.ChildNodes;
Console.WriteLine("Number of child nodes: {0}",
documentChildren.Count);
xmlDocument.LoadXml("<!-- a comment child node --><gods />");
Console.WriteLine("Number of child nodes: {0}",
documentChildren.Count);

and it will output

Number of child nodes: 0
Number of child nodes: 2
GetElementsByTagName also returns a live XmlNodeList, e.g. try

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList godElements = xmlDocument.GetElementsByTagName("god");
Console.WriteLine("Number of god elements: {0}", godElements.Count);
xmlDocument.LoadXml("<gods><god>Kibo</god><god>Xibo</god></gods>");
Console.WriteLine("Number of god elements: {0}", godElements.Count);

and it will output

Number of god elements: 0
Number of god elements: 2

However the implementation of the live XmlNodeList for
GetElementsByTagName is badly done and can give you serious performance
problems, see
<http://support.microsoft.com/kb/823928/en-us>
The XmlNodeList returned by SelectNodes is not live e.g. if you check

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList godElements = xmlDocument.SelectNodes("gods/god");
Console.WriteLine("Number of god elements: {0}", godElements.Count);
xmlDocument.LoadXml("<gods><god>Kibo</god><god>Xibo</god></gods>");
Console.WriteLine("Number of god elements: {0}", godElements.Count);

then the output is

Number of god elements: 0
Number of god elements: 0
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 23 '06 #2
Ok, that tells me what I needed to know. Thanks.

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:u8****************@TK2MSFTNGP05.phx.gbl...


SkyHook wrote:
1. Under the topic "Select Nodes Using XPath Navigation" it says:
"All XmlNodeList objects are synchronized with the underlying document,
therefore if you ... modify the value of a node, that node is updated in
the document it came from."

2. Under the topic "XmlNode.SelectNodes Method (String)" it says:
"The XmlNodeList should not be expected to be connected "live" to the XML
document. That is, changes that appear in the XML diocument may not
appear in the XmlNodeList, and vice versa."


You are right, the documentation is misleading. With .NET some
XmlNodeLists returned are "live", e.g. ChildNodes is live, try

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList documentChildren = xmlDocument.ChildNodes;
Console.WriteLine("Number of child nodes: {0}",
documentChildren.Count);
xmlDocument.LoadXml("<!-- a comment child node --><gods />");
Console.WriteLine("Number of child nodes: {0}",
documentChildren.Count);

and it will output

Number of child nodes: 0
Number of child nodes: 2
GetElementsByTagName also returns a live XmlNodeList, e.g. try

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList godElements = xmlDocument.GetElementsByTagName("god");
Console.WriteLine("Number of god elements: {0}", godElements.Count);
xmlDocument.LoadXml("<gods><god>Kibo</god><god>Xibo</god></gods>");
Console.WriteLine("Number of god elements: {0}", godElements.Count);

and it will output

Number of god elements: 0
Number of god elements: 2

However the implementation of the live XmlNodeList for
GetElementsByTagName is badly done and can give you serious performance
problems, see
<http://support.microsoft.com/kb/823928/en-us>
The XmlNodeList returned by SelectNodes is not live e.g. if you check

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList godElements = xmlDocument.SelectNodes("gods/god");
Console.WriteLine("Number of god elements: {0}", godElements.Count);
xmlDocument.LoadXml("<gods><god>Kibo</god><god>Xibo</god></gods>");
Console.WriteLine("Number of god elements: {0}", godElements.Count);

then the output is

Number of god elements: 0
Number of god elements: 0
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

May 23 '06 #3
That's helpful, but one question remains: does updating a node in an
XmlNodeList returned from SelectNodes cause a "live" update to the
XmlDocument?

The Help from SelectNodes says "...changes that appear in the XML diocument
may not appear in the XmlNodeList, and vice versa." "Vice versa" implies to
me that if you make changes to the XmlNodeList, you can't count on them being
reflected in the XmlDocument.

And yet the code example from the same Help entry (see below) does exactly
that - it changes nodes in a NodeList returned from SelectNodes, and then
writes out the XmlDocument (expecting it to have been changed).

Any insight?

----------------------------------------
using System;
using System.IO;
using System.Xml;

public class Sample {

public static void Main() {

XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");

XmlNodeList nodeList;
XmlNode root = doc.DocumentElement;

nodeList=root.SelectNodes("descendant::book[author/last-name='Austen']");

//Change the price on the books.
foreach (XmlNode book in nodeList)
{
book.LastChild.InnerText="15.95";
}

Console.WriteLine("Display the modified XML document....");
doc.Save(Console.Out);

}
}

"Martin Honnen" wrote:


SkyHook wrote:
1. Under the topic "Select Nodes Using XPath Navigation" it says:
"All XmlNodeList objects are synchronized with the underlying document,
therefore if you ... modify the value of a node, that node is updated in the
document it came from."

2. Under the topic "XmlNode.SelectNodes Method (String)" it says:
"The XmlNodeList should not be expected to be connected "live" to the XML
document. That is, changes that appear in the XML diocument may not appear
in the XmlNodeList, and vice versa."


You are right, the documentation is misleading. With .NET some
XmlNodeLists returned are "live", e.g. ChildNodes is live, try

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList documentChildren = xmlDocument.ChildNodes;
Console.WriteLine("Number of child nodes: {0}",
documentChildren.Count);
xmlDocument.LoadXml("<!-- a comment child node --><gods />");
Console.WriteLine("Number of child nodes: {0}",
documentChildren.Count);

and it will output

Number of child nodes: 0
Number of child nodes: 2
GetElementsByTagName also returns a live XmlNodeList, e.g. try

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList godElements = xmlDocument.GetElementsByTagName("god");
Console.WriteLine("Number of god elements: {0}", godElements.Count);
xmlDocument.LoadXml("<gods><god>Kibo</god><god>Xibo</god></gods>");
Console.WriteLine("Number of god elements: {0}", godElements.Count);

and it will output

Number of god elements: 0
Number of god elements: 2

However the implementation of the live XmlNodeList for
GetElementsByTagName is badly done and can give you serious performance
problems, see
<http://support.microsoft.com/kb/823928/en-us>
The XmlNodeList returned by SelectNodes is not live e.g. if you check

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList godElements = xmlDocument.SelectNodes("gods/god");
Console.WriteLine("Number of god elements: {0}", godElements.Count);
xmlDocument.LoadXml("<gods><god>Kibo</god><god>Xibo</god></gods>");
Console.WriteLine("Number of god elements: {0}", godElements.Count);

then the output is

Number of god elements: 0
Number of god elements: 0
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jun 23 '06 #4
Short answer: yes

Long answer: there is but one 'node' SelectNodes does not return a
copy, or some view, it returns the actual node.
I believe the help is trying to indicate that changes to the document
may not cause nodes to be added/removed from the XmlNodeList result of
SelectNodes. For example, if I select "//foo[@a=1]" (all elements
'foo' inthe null namespace with attribute 'a' with value '1'), then if
I change the value of an 'a' attribute on a 'foo' element, that
may-or-may-not result in a change to the nodes reported by the
XmlNodeList.

-derekdb

Tom G wrote:
That's helpful, but one question remains: does updating a node in an
XmlNodeList returned from SelectNodes cause a "live" update to the
XmlDocument?

The Help from SelectNodes says "...changes that appear in the XML diocument
may not appear in the XmlNodeList, and vice versa." "Vice versa" implies to
me that if you make changes to the XmlNodeList, you can't count on them being
reflected in the XmlDocument.

And yet the code example from the same Help entry (see below) does exactly
that - it changes nodes in a NodeList returned from SelectNodes, and then
writes out the XmlDocument (expecting it to have been changed).

Any insight?

----------------------------------------
using System;
using System.IO;
using System.Xml;

public class Sample {

public static void Main() {

XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");

XmlNodeList nodeList;
XmlNode root = doc.DocumentElement;

nodeList=root.SelectNodes("descendant::book[author/last-name='Austen']");

//Change the price on the books.
foreach (XmlNode book in nodeList)
{
book.LastChild.InnerText="15.95";
}

Console.WriteLine("Display the modified XML document....");
doc.Save(Console.Out);

}
}

"Martin Honnen" wrote:


SkyHook wrote:
1. Under the topic "Select Nodes Using XPath Navigation" it says:
"All XmlNodeList objects are synchronized with the underlying document,
therefore if you ... modify the value of a node, that node is updated in the
document it came from."

2. Under the topic "XmlNode.SelectNodes Method (String)" it says:
"The XmlNodeList should not be expected to be connected "live" to the XML
document. That is, changes that appear in the XML diocument may not appear
in the XmlNodeList, and vice versa."


You are right, the documentation is misleading. With .NET some
XmlNodeLists returned are "live", e.g. ChildNodes is live, try

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList documentChildren = xmlDocument.ChildNodes;
Console.WriteLine("Number of child nodes: {0}",
documentChildren.Count);
xmlDocument.LoadXml("<!-- a comment child node --><gods />");
Console.WriteLine("Number of child nodes: {0}",
documentChildren.Count);

and it will output

Number of child nodes: 0
Number of child nodes: 2
GetElementsByTagName also returns a live XmlNodeList, e.g. try

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList godElements = xmlDocument.GetElementsByTagName("god");
Console.WriteLine("Number of god elements: {0}", godElements.Count);
xmlDocument.LoadXml("<gods><god>Kibo</god><god>Xibo</god></gods>");
Console.WriteLine("Number of god elements: {0}", godElements.Count);

and it will output

Number of god elements: 0
Number of god elements: 2

However the implementation of the live XmlNodeList for
GetElementsByTagName is badly done and can give you serious performance
problems, see
<http://support.microsoft.com/kb/823928/en-us>
The XmlNodeList returned by SelectNodes is not live e.g. if you check

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList godElements = xmlDocument.SelectNodes("gods/god");
Console.WriteLine("Number of god elements: {0}", godElements.Count);
xmlDocument.LoadXml("<gods><god>Kibo</god><god>Xibo</god></gods>");
Console.WriteLine("Number of god elements: {0}", godElements.Count);

then the output is

Number of god elements: 0
Number of god elements: 0
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Jun 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Neil | last post by:
I'm trying to get a list of nodes using the XMLNodeList.SelectNodes( xpath ) method. I want a list of criteria nodes where the attribute notModifiable is false or doesn't exist. Anyone know how to do...
1
by: Robert | last post by:
I am having a problem selecting nodes using the XMLnodelist Selectnodes using XPATH when I use XML SPY is successfully queries but when is use VB.net it comes up with nothing. Here is my code ...
2
by: ree32 | last post by:
When I import an xml document in Visual studio and Genereate as schema from it, and create a dataset from it, it adds this line into to the root element of my xml file -...
2
by: Jeff Jarrell | last post by:
I can't seem to get an xpath expression proper to get the references out of the *.vbproj project file in VS2005. the project is loaded into an XMLDocument _dom = new xmldocument...
2
by: Jon | last post by:
I got a question on executing the xpath. Can someone let me know which way is faster for the following two scenarios (one with XmlDocument and the other using XmlDocument.CreateNavigator())? ...
0
by: dotnetnoob | last post by:
Dim strFacilitFile As String = Func_Facilit(strJobSite) Dim xTempltDoc As New Xml.XmlDocument Dim xGpDoc As New Xml.XmlDocument Dim y As Integer = 0 'Dim intNxtInstNumb As Integer =...
9
by: DBC User | last post by:
Hi, I have an xml and I am able to use xpath to identify each node that statisfy the selection criteria. I got the node list. I would like to know is it possible to do the following for the...
3
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; We have a TreeView that represents an xml file (or it's schema is a more accurate statement). We want to have a double click on a node in the tree generate the XPath to get to that node. ...
2
by: =?Utf-8?B?Z2lkZHk=?= | last post by:
hi, !!Please help! I've been at this for a few hours now! I'm probably doing something silly. heres my xml document: <Log> <entry user="Gideon" date="11/6/2008" time="10:14 AM"...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.