XmlNode.SelectSingleNode() Question

S

Steve Harclerode

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.SelectSingleNode(@"//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
 
M

Martin Honnen

Steve said:
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 SelectSingleNode should return nodes that are somehow
disconnected from the owning XmlDocument instance.
 
F

Frank Rizzo

Martin said:
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 SelectSingleNode 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 SelectSingleNode, don't expect the changes to
the file to show up in your results. I think that is what's meant by
"live".
 
S

Steve Harclerode

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

Thanks.

- Steve
 
G

globally_unique_identifier

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.
 
M

Martin Honnen

Frank said:
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 SelectSingleNode, 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
SelectSingleNode or SelectNodes but to any way to navigate to or access
a node in the DOM tree.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top