xml-----------node 与 element

node 包含xml的全部内容 

比如<book>算法入门</book>

算法入门也相当于一个 节点 node,类型是text 

而book可以称为元素

.net中的定义

Element:



Node:

System.Object
System.Xml.XmlNode
System.Xml.XmlAttribute
System.Xml.XmlDocument
System.Xml.XmlDocumentFragment
System.Xml.XmlEntity
System.Xml.XmlLinkedNode
System.Xml.XmlNotation


在W3C中node 有各种类型。。。。。。。。。。。。。。。。。。。。




节点类型

下面的表格列出了不同的 W3C 节点类型,以及它们可拥有的子元素:

节点类型 描述 子元素
Document 表示整个文档(DOM 树的根节点)
  • Element (max. one)
  • ProcessingInstruction
  • Comment
  • DocumentType
DocumentFragment 表示轻量级的 Document 对象,其中容纳了一部分文档。
  • ProcessingInstruction
  • Comment
  • Text
  • CDATASection
  • EntityReference
DocumentType 向为文档定义的实体提供接口。 None
ProcessingInstruction 表示处理指令。 None
EntityReference 表示实体引用元素。
  • ProcessingInstruction
  • Comment
  • Text
  • CDATASection
  • EntityReference
Element 表示 element(元素)元素
  • Text
  • Comment
  • ProcessingInstruction
  • CDATASection
  • EntityReference
Attr 表示属性。
  • Text
  • EntityReference
Text 表示元素或属性中的文本内容。 None
CDATASection 表示文档中的 CDATA 区段(文本不会被解析器解析) None
Comment 表示注释。 None
Entity 表示实体。
  • ProcessingInstruction
  • Comment
  • Text
  • CDATASection
  • EntityReference
Notation 表示在 DTD 中声明的符号。 None

综上这样遍历时会获得 元素 文本,注释节点 (属性获取不到)

//利用递归获取xml的所有节点(元素也是节点)
        public void RecurseXml(XmlNode  root,int index)
        {
            if (root == null) 
            {
                return;
            }
            if (root is XmlElement) 
            {
                tbxContent.Text += root.Name.PadLeft(root.Name.Length + index)+Environment.NewLine;//换行
                
                if (root.HasChildNodes) 
                {
                    RecurseXml(root.FirstChild,index+1);
                }
                if(root.NextSibling!=null)
                {
                    RecurseXml(root.NextSibling,index+2);
                }
            }
            else if(root is XmlText)
            {
                string text=((XmlText)root).Value;
                tbxContent.Text+=text.PadLeft(text.Length+index)+Environment.NewLine;
            }
            else if (root is XmlAttribute) 
            {
                string text = ((XmlAttribute)root).Value;
                tbxContent.Text += text.PadLeft(text.Length + index) + Environment.NewLine;
            }
            else if (root is XmlComment) 
            {
                string text = ((XmlComment)root).Value;
                tbxContent.Text += text.PadLeft(text.Length + index) + Environment.NewLine;
            }
        }



原文

<?xml version="1.0" encoding="UTF-8"?>
-<phone> -<xphone name="motox"> <price>$200</price> <color>black</color> -<test>
<!--this is comment -->
</test> </xphone> <xiaomi name="xiaomi" price="1999"/> -<mx3> <color>green</color> <price>$350</price> </mx3> -<motox> <xx>sssssssssssss</xx> <xxx>KKK</xxx> </motox> </phone>

结果:




phone
 xphone
  price
   $200
    color
     black
      test
       this is comment 
   xiaomi
     mx3
      color
       green
        price
         $350
       motox
        xx
         sssssssssssss
          xxx
           KKK



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值