xml-----------node 与 element
2014-03-10 21:02
281 查看
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 节点类型,以及它们可拥有的子元素:
综上这样遍历时会获得 元素 文本,注释节点 (属性获取不到)
//利用递归获取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;
}
}
原文
结果:
phone
xphone
price
$200
color
black
test
this is comment
xiaomi
mx3
color
green
price
$350
motox
xx
sssssssssssss
xxx
KKK
比如<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 | 向为文档定义的实体提供接口。 | 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
相关文章推荐
- XML中Node和Element的区别
- C#中读取XML错误解决: System.Xml.XmlException: “Element”是无效的 XmlNodeType。
- XML中node和element的区别
- XML中node和element的区别
- [转]XML中元素(Element)与节点(Node)的区别
- xml中Element与Node区别
- MyBatis-3.4.2-源码分析9:XML解析之databaseIdProviderElement(root.evalNode("databaseIdProvider"))
- XML中Element和Node的区别
- xml中的元素(element)和节点(node)——C#
- XML(一) ---节点node和元素element的区别
- 20100603 学习记录:常用类型--XMLDocument XMLNode XMLElement XMLAttribute XMLFragment XML
- MyBatis-3.4.2-源码分析12:XML解析之mapperElement(root.evalNode("mappers"))
- xml中Node和Element的区别
- python xml test 遍历element attr textNode
- xml中Node和Element的区别
- xml中Node和Element的区别
- Difference between node and element in xml
- Convert XElement to XmlNode (and Convert XmlNode to XElement){转载}
- Convert XElement to XmlNode (and Convert XmlNode to XElement){转载}
- MyBatis-3.4.2-源码分析1:解析XML之propertiesElement(root.evalNode("properties"))