Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm creating a ping application for school with an XML full of URLs. I lost an hour because of XmlNode.Value was resulting in a null.

Then I changed it into InnerText and it worked fine.

Now I was wonder what's the difference because MSDN says that .Value returns the value of the node and InnerText returns the concatenated values of the node and all its child nodes.

Can someone explain this for me please?

<sites>
<site>
    <url>www.test.be</url>
    <email>test@test.be</email>
</site>
<site>
    <url>www.temp.be</url>
    <email>temp@temp.be</email>
</site>
<site>
    <url>www.lorim.ipsum</url>
    <email>interim.address@domain.com</email>
</site></sites>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
354 views
Welcome To Ask or Share your Answers For Others

1 Answer

If, for example, your XML looks like <Foo>Bar</Foo> then "Bar" is actually considered a separate node: an XmlText node (sub-classed from XmlNode). The Value property of that XmlText node would be "Bar".

"Foo" is considered to be an XmlElement (also sub-classed from XmlNode). XmlNode.Value returns different things based on the type of node it is. See this table which shows that Value always returns null for Element nodes.

The InnerText of the Foo node returns "Bar" because it concatenates the values of its children (in this case, only the one XmlText node).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...