node 包含xml的全部内容
比如<book>算法入门</book>
算法入门也相当于一个 节点 node,类型是text
而book可以称为元素
.net中的定义
Element:
Node:
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 树的根节点) |
|
DocumentFragment | 表示轻量级的 Document 对象,其中容纳了一部分文档。 |
|
DocumentType | 向为文档定义的实体提供接口。 | None |
ProcessingInstruction | 表示处理指令。 | None |
EntityReference | 表示实体引用元素。 |
|
Element | 表示 element(元素)元素 |
|
Attr | 表示属性。 |
|
Text | 表示元素或属性中的文本内容。 | None |
CDATASection | 表示文档中的 CDATA 区段(文本不会被解析器解析) | None |
Comment | 表示注释。 | None |
Entity | 表示实体。 |
|
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