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

来源:互联网 发布:淘宝爆款打造方法2016 编辑:程序博客网 时间:2024/05/16 00:46

node 包含xml的全部内容 

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

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

而book可以称为元素

.net中的定义

继承层次结构
Element:

System.Object
System.Xml.XmlNode
System.Xml.XmlLinkedNode
System.Xml.XmlElement


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向为文档定义的实体提供接口。NoneProcessingInstruction表示处理指令。NoneEntityReference表示实体引用元素。
  • ProcessingInstruction
  • Comment
  • Text
  • CDATASection
  • EntityReference
Element表示 element(元素)元素
  • Text
  • Comment
  • ProcessingInstruction
  • CDATASection
  • EntityReference
Attr表示属性。
  • Text
  • EntityReference
Text表示元素或属性中的文本内容。NoneCDATASection表示文档中的 CDATA 区段(文本不会被解析器解析)NoneComment表示注释。NoneEntity表示实体。
  • 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
原创粉丝点击