The difference between value and innertext in XmlNode

Source: Internet
Author: User

This is a question I think many people have encountered when using. NET to manipulate XML documents, first look at MSDN for explanations of these two properties:

Xmlnode.value: Gets or sets the value of the node.

Xmlnode.innertext: Gets or sets the concatenated value of the node and all its child nodes.

Just look at these two definitions is not still a little confused, below we use examples to illustrate:

1. When the operation node is a leaf node:
XmlDocument xdoc=new XmlDocument ();
Xdoc.loadxml (@ "<SmartCoder>
                <Coder>
                    <Name>Tiramisu</Name>
                    <Age>25</Age>
                </Coder>
            </SmartCoder> ");
XmlNode root=xdoc.documentelement;
XmlNode Namenode=root. selectSingleNode ("Coder/name");  Get Name Node
Console.WriteLine (Namenode.value);
Console.WriteLine (Namenode.innertext);

The output results are as follows:

Null

Tiramisu

2. When the action node is the parent node:
XmlDocument xdoc=new XmlDocument ();
Xdoc.loadxml (@ "<SmartCoder>
                <Coder>
                    <Name>Tiramisu</Name>
                    <Age>25</Age>
                </Coder>
            </SmartCoder> ");
XmlNode root=xdoc.documentelement;
XmlNode Codernode=root. selectSingleNode ("coder");  Get Name Node
Console.WriteLine (Codernode.value);
Console.WriteLine (Codernode.innertext);

The output results are as follows:

Null

Tiramisu25

3. When the Operation node is an attribute:
XmlDocument xdoc=new XmlDocument ();
Xdoc.loadxml (@ "<SmartCoder>
                <coder englishname= ' Benjamin ' >
                    <Name>Tiramisu</Name>
                    <Age>25</Age>
                </Coder>
            </SmartCoder> ");
XmlNode root=xdoc.documentelement;
XmlNode Codernode=root. selectSingleNode ("coder");  Get Name Node
Console.WriteLine (codernode.attributes["Englishname"). Value);
Console.WriteLine (codernode.attributes["Englishname"). InnerText);

Or

XmlDocument xdoc=new XmlDocument ();
Xdoc.loadxml (@ "<SmartCoder>
                <coder englishname= ' Benjamin ' >
                    <Name>Tiramisu</Name>
                    <Age>25</Age>
                </Coder>
            </SmartCoder> ");
XmlNode root=xdoc.documentelement;
XmlNode Engnameattr=root. selectSingleNode ("coder/@EnglishName");  Get Name Node
Console.WriteLine (Engnameattr.value);
Console.WriteLine (Engnameattr.innertext);

Output Result:

Benjamin
Benjamin

In the example code above, we use the XPath syntax to find DOM elements, and more XPath syntax information, please refer to them yourself.

As we can see from the example, InnerText will stitch the text content of the node and its child elements (what the angle brackets contain) together as the return value, while value does not, either the parent node or the child node, the return value is null , and when the operation's node type is a property, The return value of value is the same as innertext. In fact, the return value of value is related to the node type (NodeType), and the following is the type of node listed in MSDN and the return value of Xmlnode.value:

Type Value
Attribute The value of the property
Cdatasection CDATA the contents of the section.
Comment The content of the comment
Document Null
DocumentFragment Null
DocumentType Null
Element Null. You can use the Xmlelement.innertext or Xmlelement.innerxml property to access the value of an element node.
Entity Null
EntityReference Null
Notation Null
ProcessingInstruction All content (excluding instruction targets).
Text Content of the text node
SignificantWhitespace White space characters. Whitespace can consist of one or more empty characters, carriage returns, line feeds, or tabs.
Whitespace White space characters. Whitespace can consist of one or more empty characters, carriage returns, line feeds, or tabs.
XmlDeclaration The content of the declaration (that is, everything between <?xml and?>).
The node types we get in Example 1, 2 are Element, so Xmlnode.valueThe return value is NULL

The difference between value and innertext in XmlNode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.