XmlNode.SelectSingleNode() Question

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

    XmlNode.SelectSingleNode() Question

    I saw something in the Microsoft documentation for SelectNodes() that I've
    never noticed before today:

    -----
    Return Value
    The first XmlNode that matches the XPath query or a null reference (Nothing
    in Visual Basic) if no matching node is found. The XmlNode should not be
    expected to be connected "live" to the XML document. That is, changes that
    appear in the XML document may not appear in the XmlNode, and vice versa.
    -----

    If I'm reading this correctly, this means that you can't expect code like
    this to work:

    XmlElement myval = xmlDocument.Sel ectSingleNode(@ "//SomeElement") as
    XmlElement;
    if (myval != null) {
    myval.InnerText = "my content";
    }

    I see examples like the above all over the place...

    Am I reading the documentation correctly? If so, what's the "correct" way to
    update nodes in an XmlDocument?

    Thanks,
    Steve


  • Martin Honnen

    #2
    Re: XmlNode.SelectS ingleNode() Question

    Steve Harclerode wrote:
    I saw something in the Microsoft documentation for SelectNodes() that I've
    never noticed before today:
    >
    -----
    Return Value
    The first XmlNode that matches the XPath query or a null reference (Nothing
    in Visual Basic) if no matching node is found. The XmlNode should not be
    expected to be connected "live" to the XML document. That is, changes that
    appear in the XML document may not appear in the XmlNode, and vice versa.
    To me that comment does not make sense, DOM/XmlDocument is supposed to
    allow you to manipulate/edit/delete nodes and I don't see why
    SelectNodes or SelectSingleNod e should return nodes that are somehow
    disconnected from the owning XmlDocument instance.




    --

    Martin Honnen --- MVP XML

    Comment

    • Frank Rizzo

      #3
      Re: XmlNode.SelectS ingleNode() Question

      Martin Honnen wrote:
      Steve Harclerode wrote:
      >I saw something in the Microsoft documentation for SelectNodes() that
      >I've never noticed before today:
      >>
      >-----
      >Return Value
      >The first XmlNode that matches the XPath query or a null reference
      >(Nothing in Visual Basic) if no matching node is found. The XmlNode
      >should not be expected to be connected "live" to the XML document.
      >That is, changes that appear in the XML document may not appear in the
      >XmlNode, and vice versa.
      >
      To me that comment does not make sense, DOM/XmlDocument is supposed to
      allow you to manipulate/edit/delete nodes and I don't see why
      SelectNodes or SelectSingleNod e should return nodes that are somehow
      disconnected from the owning XmlDocument instance.
      I think it makes sense. If I am reading it correctly, it means that if
      you load an xml file into the XML DOM, then make changes to the file,
      then query the XML DOM via SelectSingleNod e, don't expect the changes to
      the file to show up in your results. I think that is what's meant by
      "live".


      >
      >
      >
      >

      Comment

      • Steve Harclerode

        #4
        Re: XmlNode.SelectS ingleNode() Question

        Aha, I think you're right. I was reading "XML Document" as "XmlDocumen t",
        which is not the same.

        Thanks.

        - Steve

        "Frank Rizzo" <none@none.comw rote in message
        news:%23NfdjAKR HHA.4476@TK2MSF TNGP05.phx.gbl. ..
        Martin Honnen wrote:
        I think it makes sense. If I am reading it correctly, it means that if
        you load an xml file into the XML DOM, then make changes to the file, then
        query the XML DOM via SelectSingleNod e, don't expect the changes to the
        file to show up in your results. I think that is what's meant by "live".

        Comment

        • globally_unique_identifier@yahoo.com

          #5
          Re: XmlNode.SelectS ingleNode() Question

          What it means is when you load an XML file into a Document Object
          Model, it is loaded into memory. Any XPath query or modification from
          that point forward is working on the DOM that's in memory. A change
          to the XML file won't be reflected in a query until the file is
          reloaded. Any changes you make aren't persisted until you write the
          DOM back to the file. At least, that what I think they're trying to
          say. The last sentence is misleading, IMO.

          Comment

          • Martin Honnen

            #6
            Re: XmlNode.SelectS ingleNode() Question

            Frank Rizzo wrote:
            I think it makes sense. If I am reading it correctly, it means that if
            you load an xml file into the XML DOM, then make changes to the file,
            then query the XML DOM via SelectSingleNod e, don't expect the changes to
            the file to show up in your results. I think that is what's meant by
            "live".
            If "changes to the XML document" means changes to an XML file on disk
            then that comment makes sense. But it is then not specific to
            SelectSingleNod e or SelectNodes but to any way to navigate to or access
            a node in the DOM tree.


            --

            Martin Honnen --- MVP XML

            Comment

            Working...