SelectNodes. For example, if I select "//foo[@a=1]" (all elements
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/ > >