xmlNode.InnerText vs. xmlNode.Value

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I've loaded the following xml into an XMLDocument object:

<?xml version="1.0" ?>
- <Preferences>
<Institution>Argh</Institution>
<Speaker>Chigier, Ben (1234)</Speaker>
<Worktype>Addendum</Worktype>
<speakerCode>1234</speakerCode>
</Preferences>

I then try to retrieve the value of the Institution node as follows:

string s = myxmldoc.SelectSingleNode("/Preferences/Institution").Value;

However, s is null after the call. But if I change the call to the
following, s is what I'd expect: "Argh".

string s = myxmldoc.SelectSingleNode("/Preferences/Institution").InnerText;

Anyone know what's going on?

Thanks...

Dan
 
Dan said:
string s = myxmldoc.SelectSingleNode("/Preferences/Institution").Value;

However, s is null after the call. But if I change the call to the
following, s is what I'd expect: "Argh".

string s = myxmldoc.SelectSingleNode("/Preferences/Institution").InnerText;

Anyone know what's going on?

InnerText goes through the descendant XmlText node(s) of an
XmlNode, and concatenates their value(s) together. It is the
Value property of the XmlText node(s) that contains text.

There are some questions to ask yourself about XmlElement,
and what the representation of it's Value should be:

If it had a value, what would it be? LocalName?
NamespaceURI? QName? InnerText? InnerXml?
Does an Element whose InnerXml is "<b>Hey</b>"
have a Value different from one whose InnerXml is
"Hey," and should it be different?

The Value property of an XmlElement is always null because
XmlElements have no representable value.


Derek Harmon
 
Dan said:
I've loaded the following xml into an XMLDocument object:

<?xml version="1.0" ?>
- <Preferences>
<Institution>Argh</Institution>
<Speaker>Chigier, Ben (1234)</Speaker>
<Worktype>Addendum</Worktype>
<speakerCode>1234</speakerCode>
</Preferences>

I then try to retrieve the value of the Institution node as follows:

string s = myxmldoc.SelectSingleNode("/Preferences/Institution").Value;

However, s is null after the call. But if I change the call to the
following, s is what I'd expect: "Argh".

string s = myxmldoc.SelectSingleNode("/Preferences/Institution").InnerText;

Anyone know what's going on?

Well simply what you expect to be the value of an element is not the
Value property in the DOM (document object model) that .NET implements.
In that model there is a base class XmlNode which already has the Value
property and in its documentation at
http://msdn.microsoft.com/library/d...html/frlrfSystemXmlXmlNodeClassValueTopic.asp
details are given what the Value is for the different type of nodes. For
Element nodes it says
"A null reference (Nothing). You can use the XmlElement.InnerText or
XmlElement.InnerXml properties to access the value of the element node."
 
Back
Top