zoukankan      html  css  js  c++  java
  • XmlNode中Value和InnerText的区别

    XmlNode中Value和InnerText的区别

     

    这个问题我想很多人在使用.NET 操作 Xml 文档时都遇到过,先看一下MSDN里对这两个属性的解释:

    XmlNode.Value:获取或设置节点的值。

    XmlNode.InnerText:获取或设置节点及其所有子节点的串联值。

    只看这两个定义是不是还是有点迷糊,下面我们用实例来作说明:

    1.当操作节点是叶子节点时:
    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");  // 获取Name节点
    Console.WriteLine(nameNode.Value);
    Console.WriteLine(nameNode.InnerText);

    输出结果如下:

    null

    Tiramisu

    2.当操作节点是父结点时:
    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");  // 获取Name节点
    Console.WriteLine(coderNode.Value);
    Console.WriteLine(coderNode.InnerText);

    输出结果如下:

    null

    Tiramisu25

    3.当操作节点是属性时:
    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");  // 获取Name节点
    Console.WriteLine(coderNode.Attributes["EnglishName"].Value);
    Console.WriteLine(coderNode.Attributes["EnglishName"].InnerText);

    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");  // 获取Name节点
    Console.WriteLine(engNameAttr.Value);
    Console.WriteLine(engNameAttr.InnerText);

    输出结果:

    Benjamin 
    Benjamin

    上文的示例代码中,我们使用了XPath语法来查找DOM元素,更多的XPath语法信息,大家请自行查阅。

    从示例中我们可以看出,InnerText会把节点及其子元素的文本内容(尖括号所包含的内容)拼接起来作为返回值;而Value则不然,无论是父节点还是子节点,返回值都为 null ,而当操作的节点类型为属性时,Value的返回值与InnerText相同。其实,Value的返回值,与节点类型(NodeType)相关,下面是MSDN中列出的节点类型及 XmlNode.Value 的返回值:

    类型
    Attribute 属性的值
    CDATASection CDATA 节的内容。
    Comment 注释的内容
    Document null
    DocumentFragment null
    DocumentType null
    Element null . 您可以使用 XmlElement.InnerText 或 XmlElement.InnerXml 属性访问元素节点的值。
    Entity null
    EntityReference null
    Notation null
    ProcessingInstruction 全部内容(不包括指令目标)。
    Text 文本节点的内容
    SignificantWhitespace 空白字符。 空白可由一个或多个空格字符、回车符、换行符或制表符组成。
    Whitespace 空白字符。 空白可由一个或多个空格字符、回车符、换行符或制表符组成。
    XmlDeclaration 声明的内容(即在 <?xml 和 ?> 之间的所有内容)。
    示例1、2中我们获取的节点类型都是 Element ,所以 XmlNode.Value 的返回值是 null
  • 相关阅读:
    MVC模式-----struts2框架(2)
    MVC模式-----struts2框架
    html的<h>标签
    jsp脚本元素
    LeetCode "Paint House"
    LeetCode "Longest Substring with At Most Two Distinct Characters"
    LeetCode "Graph Valid Tree"
    LeetCode "Shortest Word Distance"
    LeetCode "Verify Preorder Sequence in Binary Search Tree"
    LeetCode "Binary Tree Upside Down"
  • 原文地址:https://www.cnblogs.com/Alex80/p/4431510.html
Copyright © 2011-2022 走看看