Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

c# - XmlNode Value vs InnerText

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>[email protected]</email>
</site>
<site>
    <url>www.temp.be</url>
    <email>[email protected]</email>
</site>
<site>
    <url>www.lorim.ipsum</url>
    <email>[email protected]</email>
</site></sites>
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...