分享

xmlnode 的value与innertext区别

 隐形的翅膀 2008-02-28
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,他返回是属性的值

 

 
 
 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多