XPath XmlNodeList documentation - apparent contradition

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SkyHook

    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


  • Martin Honnen

    #2
    Re: XPath XmlNodeList documentation - apparent contradition



    SkyHook wrote:
    [color=blue]
    > 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."[/color]

    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

    Comment

    • SkyHook

      #3
      Re: XPath XmlNodeList documentation - apparent contradition

      Ok, that tells me what I needed to know. Thanks.

      "Martin Honnen" <mahotrash@yaho o.de> wrote in message
      news:u8%230THmf GHA.3364@TK2MSF TNGP05.phx.gbl. ..[color=blue]
      >
      >
      > SkyHook wrote:
      >[color=green]
      >> 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."[/color]
      >
      > 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/[/color]


      Comment

      • Tom G

        #4
        Re: XPath XmlNodeList documentation - apparent contradition

        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:
        [color=blue]
        >
        >
        > SkyHook wrote:
        >[color=green]
        > > 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."[/color]
        >
        > 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/
        >[/color]

        Comment

        • zuligag@gmail.com

          #5
          Re: XPath XmlNodeList documentation - apparent contradition

          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:[color=blue]
          > 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:
          >[color=green]
          > >
          > >
          > > SkyHook wrote:
          > >[color=darkred]
          > > > 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."[/color]
          > >
          > > 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/
          > >[/color][/color]

          Comment

          Working...