XmlNode vs InnerText Value

I am creating a ping application for a school with XML full of URLs. I lost an hour due to XmlNode.Value , getting a null value.

Then I changed it to InnerText and it worked fine.

Now I was wondering what the difference is, since MSDN says .Value returns the value of a node and InnerText returns the concatenated values โ€‹โ€‹of the node and all its child nodes.

Can someone explain this to 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> 
+58
c # xml
Oct 24 '11 at 14:46
source share
4 answers

If, for example, your XML looks like <Foo>Bar</Foo> , then "Bar" is actually considered a separate node: a XmlText node (subclassed from XmlNode ). The Value property of this XmlText node will be "Bar".

"Foo" is considered an XmlElement (also subclassed from XmlNode ). XmlNode.Value returns different things depending on the type of node. See this table , which shows that Value always returns null for Element nodes.

InnerText Foo node returns "Bar" because it combines the values โ€‹โ€‹of its children (in this case, only one XmlText node).

+71
Oct 24 '11 at 15:04
source share

I had a similar situation. What I did, I selected the first child of the current node and checked if it is an XMLtext, and then displayed its value.

 XmlNodeList xNList = xDOC.SelectNodes("//" + XMLElementname); foreach (XmlNode xNode in xNList) { if (xNode.ChildNodes.Count == 1 && xNode.FirstChild.GetType().ToString() == "System.Xml.XmlText") { XMLElements.Add(xNode.FirstChild.Value); } else { XMLElements.Add("This is not a Leaf node"); } } 
+7
Aug 31 '12 at 11:27
source share

The XML specification is very picky about terminology and what constitutes a type of XML object. As already mentioned, element does not matter. This is characteristic of attribute (and probably several other types of node), because attribute has syntax that element does not matter, i.e. name='value' .

If you think you're confusing, check the difference between a descendant and a descendant or a Root node and a document element!

+1
Oct 24 '11 at 18:22
source share

Regarding MSDN , the Value property of XmlNodeType.Element returns:

zero. You can use the XmlElement.InnerText or XmlElement.InnerXml properties to access the value of a node element.

0
Oct 24 '11 at 15:05
source share



All Articles