Fork me on GitHub

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.InnerTextXmlElement.InnerXml 属性访问元素节点的值。
Entity null
EntityReference null
Notation null
ProcessingInstruction 全部内容(不包括指令目标)。
Text 文本节点的内容
SignificantWhitespace 空白字符。 空白可由一个或多个空格字符、回车符、换行符或制表符组成。
Whitespace 空白字符。 空白可由一个或多个空格字符、回车符、换行符或制表符组成。
XmlDeclaration 声明的内容(即在 <?xml 和 ?> 之间的所有内容)。
示例1、2中我们获取的节点类型都是 Element ,所以 XmlNode.Value 的返回值是 null
posted @   TimGong  阅读(9947)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示