XmlNode Value vs InnerText

XmlNode Value vs InnerText

Asked on January 12, 2019 in XML.
Add Comment


  • 7 Answer(s)

         The XML will looks like <Foo>Bar</Foo> then “Bar” is normally considered a individual node: an XmlText node sub-classed from XmlNode. The Value property of that XmlText node will be “Bar”.

    “Foo” is considered to be an XmlElement also sub-classed from XmlNode. XmlNode.Value will return various things which is based on the type of node it is. Visit the table which shows that the Value everytime returns null for Element nodes.

    The InnerText of the Foo node will return “Bar” because it connect the values of its children in this case, only the one XmlText node.

    Answered on January 12, 2019.
    Add Comment

    Try the codes:

    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");
       }
    

    }

    Answered on January 12, 2019.
    Add Comment

         The XML specification is very critical about the terminology and the constitutes and the type of XML object. As already said, element will not have a value. It will be specific to attribute and perhaps a pair of other node types because attribute had a general formula that element does not, that is name=’value’.

    Answered on January 12, 2019.
    Add Comment

    If, for example, your XML looks like Bar 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).

    Answered on February 5, 2019.
    Add Comment
    <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>
    Answered on February 25, 2019.
    Add Comment
    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");
        }
    }
    Answered on February 25, 2019.
    Add Comment

    This is my first application using XML, and right now I’m writing the function that will load a record from the XML file.
    I was having a good deal of trouble at first because apparently I was using the wrong property of an XmlNodeobject (.Value). I have since corrected the problem when I ran through aj32‘s tutorial on the topic and found that it should be using .InnerText instead. Why does that make all the difference? They return the same thing (string), the MSDN documentation on the function returns are so similar as makes no difference, but on works while the other doesn’t. If this is the case, what is the point of .Value?

    Answered on February 25, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.