469,268 Members | 920 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

xmlNode.InnerText vs. xmlNode.Value

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
Nov 12 '05 #1
3 23346
"Dan" <da*@dontspamme.com> wrote in message news:ep**************@TK2MSFTNGP15.phx.gbl...
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
Nov 12 '05 #2


Dan wrote:
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/de...ValueTopic.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."

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #3
It all depends on your NodeType.

This out outlined in the following MSDN article:

http://msdn.microsoft.com/library/de...valuetopic.asp
If you are going after the Text in a node (type Element or type Text) your
best bet is to use .InnerText

"Dan" <da*@dontspamme.com> wrote in message
news:ep**************@TK2MSFTNGP15.phx.gbl...
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

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jocelyn Kwok | last post: by
2 posts views Thread by Leszek | last post: by
5 posts views Thread by Paul | last post: by
3 posts views Thread by Dan | last post: by
2 posts views Thread by Josema | last post: by
reply views Thread by Philip Wagenaar | last post: by
5 posts views Thread by Steve Harclerode | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.