打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
xmlnode 的value与innertext区别
1。当node是叶子节点时
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<right>a</right>");
        XmlNode root = doc.DocumentElement;
        Console.WriteLine(root.InnerText);
结果是:a
 
2。当node是父节点时,将包括子节点的所有的innertext显示出来
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<root name=‘jack‘ value = ‘jacky‘ ><right>a</right><left>b</left>content</root>");
        XmlNode root = doc.DocumentElement;
        Console.WriteLine(root.InnerText);
结果是:abcontent
 
3。显示属性值
   如果想显示root的name属性值,可以通过innertext和value获得
         XmlDocument doc = new XmlDocument();
        doc.LoadXml("<root name=‘jack‘ value = ‘jacky‘ ><right id =‘007‘>a</right><left>b</left>content</root>");
        XmlNode root = doc.DocumentElement;
        XmlNode node = root.SelectSingleNode("right");
        Console.WriteLine(node.Attributes["id"].InnerText);
        Console.WriteLine(node.Attributes["id"].Value);
 
或者 XmlNode root = doc.DocumentElement;
        XmlNode node = root.SelectSingleNode("right");
         node = node.SelectSingleNode("@id");

        Console.WriteLine(node.InnerText);
        Console.WriteLine(node.Value);
 
4。当取节点的Value时,我们发现为空

        XmlNode root = doc.DocumentElement;
        XmlNode node = root.SelectSingleNode("right");
        Console.WriteLine(node.Value);//rerult is  null reference
        Console.WriteLine(node.InnerText);// result is a

 原因是:
value这个属性比较特殊,它返回的值与当前节点的nodetype有关:(MSDN)

Type

Value

Attribute

The value of the attribute.

CDATASection

The content of the CDATA Section.

Comment

The content of the comment.

Document

a null reference (Nothing in Visual Basic).

DocumentFragment

a null reference (Nothing in Visual Basic).

DocumentType

a null reference (Nothing in Visual Basic).

Element

a null reference (Nothing in Visual Basic). You can use the XmlElement.InnerText or XmlElement.InnerXml properties to access the value of the element node.

Entity

a null reference (Nothing in Visual Basic).

EntityReference

a null reference (Nothing in Visual Basic).

Notation

a null reference (Nothing in Visual Basic).

ProcessingInstruction

The entire content excluding the target.

Text

The content of the text node.

 
例4中node的nodetype是element,所以通过value返回值是空引用,而当我们取得属性的value时,因为它的nodetype为attribute,他返回是属性的值

 

 
 
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
xml
c# XML操作
C#中读取xml文件指定节点
用XML文件保存配置信息的一个类(C#)
XmlDocument 类 (System.Xml)
C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例(转载)...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服