• 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
  • 相关阅读:
    准备 FRM 考试——方法、工具与教训
    930. 和相同的二元子数组 前缀和
    1906. 查询差绝对值的最小值 前缀和
    剑指 Offer 37. 序列化二叉树 二叉树 字符串
    815. 公交路线 BFS
    518. 零钱兑换 II dp 完全背包
    1049. 最后一块石头的重量 II dp
    5779. 装包裹的最小浪费空间 二分
    5778. 使二进制字符串字符交替的最少反转次数 字符串 滑动窗口
    474. 一和零 dp
  • 原文地址:https://www.cnblogs.com/zztong/p/6695162.html
Copyright © 2020-2023  润新知