Carson
31.95
05/01/2001
MSPress
WA
图中的每个圆圈表示一个节点(称为 XmlNode 对象)。XmlNode 对象是 DOM 树中的基本对象。
XmlDocument 类(扩展 XmlNode)支持用于对整个文档执行操作(例如,将文档加载到内存中或将 XML 保存到文件中)的方法。
此外,XmlDocument 提供了查看和处理整个 XML 文档中的节点的方法。XmlNode 和 XmlDocument 都具有性能和可用性增强,并通过方法和属性执行下列操作:
1、访问和修改 DOM 特定的节点,如元素节点、实体引用节点等。属性是不属于父子关系和同辈关系的节点。属性被视为元素节点的属性,由名称和值对组成。
例如,如果存在由与元素 price 关联的 format="dollar" 组成的 XML 数据,则单词 format 是名称,format 属性的值是 dollar。为检索 price 节点的 format="dollar" 属性,可以在游标位于 price 元素节点时调用 GetAttribute 方法。
将 XML 读入内存时会创建节点。然而,并非所有节点都是同一类型。 Dim rawData =
Grease
101
10
Lawrence of Arabia
102
10
Imports System.Xml
Public Class Form1
Private Sub btnDom_Click(sender As Object, e As EventArgs) Handles btnDom.Click
'XML文档的字符串
Dim rawData =
Grease
101
10
Lawrence of Arabia
102
10
Dim xmlDoc As New XmlDocument
Dim xmlNodes As XmlNodeList
Dim xmlNode As XmlNode
Dim strNodeXml As String = ""
Dim strNodeText As String = ""
'load()是加载xml文档,LoadXml()是加载xml字符串
xmlDoc.LoadXml(rawData.ToString)
xmlNodes = xmlDoc.GetElementsByTagName("FilmOrder")
For Each xmlNode In xmlNodes
strNodeXml &= xmlNode.Name & ":" & xmlNode.InnerXml & vbCrLf
strNodeText &= xmlNode.Name & ":" & xmlNode.InnerText & vbCrLf
Next
'XmlNode.InnerXml 属性:获取或设置仅代表该节点的子节点的标记。
'XmlNode.InnerText 属性:获取或设置节点及其所有子节点的串联值。
TextBox1.Text = strNodeXml & vbCrLf & strNodeText
End Sub
End Class
Imports System.Xml
Public Class Form1
Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
Dim strXml As String
Dim strFileName As String = "3.xml"
strXml = CreateXmlFile(strFileName)
TextBox1.Text = strXml
End Sub
Private Function CreateXmlFile(ByVal strFile As String) As String
Dim xmlDoc As New XmlDocument
Dim xmlEle As XmlElement
Dim root As XmlElement = xmlDoc.CreateElement("FilmOrderList") '创建节点元素
xmlDoc.AppendChild(root) '将该节点添加进xml文档
For i As Integer = 1 To 4
xmlEle = CreateFileorder(xmlDoc, i)
root.AppendChild(xmlEle)
Next
xmlDoc.Save(strFile) '保存到文件
Return xmlDoc.OuterXml '提取xml文档中所有字符
End Function
Private Function CreateFileorder(ByVal doc As XmlDocument, ByVal n As Integer) As XmlElement
Dim id As XmlAttribute
Dim film As XmlElement, title As XmlElement, quantity As XmlElement
film = doc.CreateElement("FilmOrder") '创建FileOrder结点
id = doc.CreateAttribute("ID") '创建ID属性结点
id.Value = 100 + n
film.Attributes.Append(id) '将ID属性结点添加到FileOrder结点
title = doc.CreateElement("Title") '创建Title结点
title.InnerText = "some title here"
film.AppendChild(title) '将Title结点作为FileOrder的子结点
quantity = doc.CreateElement("Quantity")
quantity.InnerText = "here is quantity"
film.AppendChild(quantity)
Return film
End Function
Private Sub btnDom_Click(sender As Object, e As EventArgs) Handles btnDom.Click
'XML文档的字符串
Dim rawData =
Grease
101
10
Lawrence of Arabia
102
10
Dim xmlDoc As New XmlDocument
Dim xmlNodes As XmlNodeList
Dim xmlNode As XmlNode
Dim strNodeXml As String = ""
Dim strNodeText As String = ""
'load是加载xml文档,LoadXml是加载xml字符串
xmlDoc.LoadXml(rawData.ToString)
xmlNodes = xmlDoc.GetElementsByTagName("FilmOrder")
For Each xmlNode In xmlNodes
strNodeXml &= xmlNode.Name & ":" & xmlNode.InnerXml & vbCrLf
strNodeText &= xmlNode.Name & ":" & xmlNode.InnerText & vbCrLf
Next
'XmlNode.InnerXml 属性:获取或设置仅代表该节点的子节点的标记。
'XmlNode.InnerText 属性:获取或设置节点及其所有子节点的串联值。
TextBox1.Text = strNodeXml & vbCrLf & strNodeText
End Sub
End Class
上例中显示时会发现xml并不是“标准”的xml文档,故另须添加声明结点、注释结点,然后再插入即可:
Dim xmlDoc As New XmlDocument
Dim xmlEle As XmlElement
Dim xmlDecl As XmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", Nothing, Nothing)
Dim xmlComm As XmlComment = xmlDoc.CreateComment("以下是程序设置,请勿修改")
Dim root As XmlElement = xmlDoc.CreateElement("FilmOrderList") '创建节点元素
xmlDoc.CreateXmlDeclaration("1.0", Nothing, Nothing)
xmlDoc.AppendChild(root) '将该节点添加进xml文档
xmlDoc.InsertBefore(xmlComm, root)
xmlDoc.InsertBefore(xmlDecl, xmlComm)