473,608 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Select Nodes 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 4621


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.Select Nodes 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 documentChildre n = xmlDocument.Chi ldNodes;
Console.WriteLi ne("Number of child nodes: {0}",
documentChildre n.Count);
xmlDocument.Loa dXml("<!-- a comment child node --><gods />");
Console.WriteLi ne("Number of child nodes: {0}",
documentChildre n.Count);

and it will output

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

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList godElements = xmlDocument.Get ElementsByTagNa me("god");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);
xmlDocument.Loa dXml("<gods><go d>Kibo</god><god>Xibo</god></gods>");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);

and it will output

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

However the implementation of the live XmlNodeList for
GetElementsByTa gName is badly done and can give you serious performance
problems, see
<http://support.microso ft.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.Sel ectNodes("gods/god");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);
xmlDocument.Loa dXml("<gods><go d>Kibo</god><god>Xibo</god></gods>");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);

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*******@yaho o.de> wrote in message
news:u8******** ********@TK2MSF TNGP05.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.Select Nodes 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 documentChildre n = xmlDocument.Chi ldNodes;
Console.WriteLi ne("Number of child nodes: {0}",
documentChildre n.Count);
xmlDocument.Loa dXml("<!-- a comment child node --><gods />");
Console.WriteLi ne("Number of child nodes: {0}",
documentChildre n.Count);

and it will output

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

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList godElements = xmlDocument.Get ElementsByTagNa me("god");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);
xmlDocument.Loa dXml("<gods><go d>Kibo</god><god>Xibo</god></gods>");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);

and it will output

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

However the implementation of the live XmlNodeList for
GetElementsByTa gName is badly done and can give you serious performance
problems, see
<http://support.microso ft.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.Sel ectNodes("gods/god");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);
xmlDocument.Loa dXml("<gods><go d>Kibo</god><god>Xibo</god></gods>");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);

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("books ort.xml");

XmlNodeList nodeList;
XmlNode root = doc.DocumentEle ment;

nodeList=root.S electNodes("des cendant::book[author/last-name='Austen']");

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

Console.WriteLi ne("Display the modified XML document....");
doc.Save(Consol e.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.Select Nodes 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 documentChildre n = xmlDocument.Chi ldNodes;
Console.WriteLi ne("Number of child nodes: {0}",
documentChildre n.Count);
xmlDocument.Loa dXml("<!-- a comment child node --><gods />");
Console.WriteLi ne("Number of child nodes: {0}",
documentChildre n.Count);

and it will output

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

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList godElements = xmlDocument.Get ElementsByTagNa me("god");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);
xmlDocument.Loa dXml("<gods><go d>Kibo</god><god>Xibo</god></gods>");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);

and it will output

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

However the implementation of the live XmlNodeList for
GetElementsByTa gName is badly done and can give you serious performance
problems, see
<http://support.microso ft.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.Sel ectNodes("gods/god");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);
xmlDocument.Loa dXml("<gods><go d>Kibo</god><god>Xibo</god></gods>");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);

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("books ort.xml");

XmlNodeList nodeList;
XmlNode root = doc.DocumentEle ment;

nodeList=root.S electNodes("des cendant::book[author/last-name='Austen']");

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

Console.WriteLi ne("Display the modified XML document....");
doc.Save(Consol e.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.Select Nodes 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 documentChildre n = xmlDocument.Chi ldNodes;
Console.WriteLi ne("Number of child nodes: {0}",
documentChildre n.Count);
xmlDocument.Loa dXml("<!-- a comment child node --><gods />");
Console.WriteLi ne("Number of child nodes: {0}",
documentChildre n.Count);

and it will output

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

XmlDocument xmlDocument = new XmlDocument();
XmlNodeList godElements = xmlDocument.Get ElementsByTagNa me("god");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);
xmlDocument.Loa dXml("<gods><go d>Kibo</god><god>Xibo</god></gods>");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);

and it will output

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

However the implementation of the live XmlNodeList for
GetElementsByTa gName is badly done and can give you serious performance
problems, see
<http://support.microso ft.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.Sel ectNodes("gods/god");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);
xmlDocument.Loa dXml("<gods><go d>Kibo</god><god>Xibo</god></gods>");
Console.WriteLi ne("Number of god elements: {0}", godElements.Cou nt);

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
2290
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 this? So far I can grab the nodes with notModifiable equal false but I don't know how to do the second bit. My statement is like below XmlNodeList criteriaNodeList = groupNode.SelectNodes("criteria"); I need to append something to this XPath...
1
5801
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 Dim nodess As XmlNode nodess = myNode.SelectSingleNode("//Web") If nodess Is Nothing Then SQLrw.Delete()
2
2874
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 - "xmlns="http://tempuri.org/nameOfRoot.xsd" I have no idea what its pointing to & what is tempuri.org? So when this tag is in my xml tag my xpath query never works. But when I delete it work fine.
2
1909
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 _dom.load(pathToMyProjectFile) dim myNodeList as XMLNodeList myNodeList = _dom.selectNodes("some xpath expression that returns the
2
3447
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())? XmlDocument doc = new XmlDocument(); doc.Load(input); 1. XmlNodeList nodes = doc.SelectNodes(xpathexp); 2. XmlNodeList nodes = doc.CreateNavigator().Select(xpathexp);
0
1281
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 = NextInstanceNumber(strJobSite) Dim intNxtInstNumb As Integer = FacilitNxtInstNum(strFacilitFile) Do Dim intGpFlag As Integer If CInt(arlsGpYN.Item(y)) = 1 Then Dim intGpYNFlag As Integer = CInt(arlsGpYN.Item(intGpFlag))
9
3599
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 following XML <Files> <Application key="one"> <Version>1</Version> <Age>120</Age> </Application>
3
2622
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. For the full tree it's easy. We walk up the parents getting the name of each node and put a / between each so we end up with /node1/node2 or node1/node2/@attr1
2
6296
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" action="Searched" /> <entry user="Jennie" date="11/6/2008" time="10:14 AM" action="Finished her
0
8067
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8010
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8483
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5479
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3967
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4030
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2477
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.