XmlNode中Value和InnerText的区别

原文:http://blog.csdn.net/wangyizhi58/article/details/7951337

这个问题我想很多人在使用.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属性的值
CDATASectionCDATA 节的内容。
Comment注释的内容
Documentnull
DocumentFragmentnull
DocumentTypenull
Elementnull . 您可以使用 XmlElement.InnerText 或 XmlElement.InnerXml 属性访问元素节点的值。
Entitynull
EntityReferencenull
Notationnull
ProcessingInstruction全部内容(不包括指令目标)。
Text文本节点的内容
SignificantWhitespace空白字符。 空白可由一个或多个空格字符、回车符、换行符或制表符组成。
Whitespace空白字符。 空白可由一个或多个空格字符、回车符、换行符或制表符组成。
XmlDeclaration声明的内容(即在 <?xml 和 ?> 之间的所有内容)。

示例1、2中我们获取的节点类型都是 Element ,所以 XmlNode.Value 的返回值是 null

 

最后欢迎大家访问我的个人网站: 1024s

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值