转载
如何用ADO.NET操作XML
已知有一个XML文件(bookstore.xml)如下:
<?xml version=”1.0” encoding=”gb2312”?>
<bookstore>
<book genre=”fantasy” ISBN=”2-3631-4>
<title>Oberon’s Leqacy</title>
<author>Corets,Eva</author>
<pricr>5.95</price>
</book>
</bookstore>
1、往<bookstore>结点中插入一个<book>结点:
Dim xmlDoc As Xml.XmlDocument = New Xml.XmlDocument '新建一个XML文档
xmlDoc.Load("../bookstore.xml"
'加载指定的XML 数据
Dim root As Xml.XmlNode '声明 XML 文档中的单个节点
root = xmlDoc.SelectSingleNode("bookstore"
'查找<bookstore>
If root Is Nothing then
MessageBox.Show("cuowu"
End If
Dim xe1 As Xml.XmlElement '声明一个元素
xe1 = xmlDoc.CreateElement("book"
'创建一个<book>节点
xe1.SetAttribute("genre", "李赞红"
'设置该节点genre属性
xe1.SetAttribute("ISBN", "2-3631-4"
'设置该节点ISBN属性
Dim xesub1 As Xml.XmlElement
xesub1 = xmlDoc.CreateElement("title"
xesub1.InnerText = "CS从入门到精通"
'设置文本结点
xe1.AppendChild(xesub1) '添加到<book>结点中
Dim xesub2 As Xml.XmlElement
xesub2 = xmlDoc.CreateElement("author"
xesub2.InnerText = "侯捷"
xe1.AppendChild(xesub2)
Dim xesub3 As Xml.XmlElement
xesub3 = xmlDoc.CreateElement("price"
xesub3.InnerText = "58.3"
xe1.AppendChild(xesub3)
Try
root.AppendChild(xe1) '添加到<bookstore>结点中
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
xmlDoc.Save("../bookstore.xml"
--结果为:------------------------------------------------------------------------------------------------------------
<?xml version="1.0"
encoding="gb2312"?>
<bookstore>
<book genre="fantasy"
ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="李赞红"
ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>
2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。
Dim xmlDoc As Xml.XmlDocument = New Xml.XmlDocument
xmlDoc.Load("../bookstore.xml"
Dim nodeList As Xml.XmlNodeList
'获取bookstore结点的所有子结点
nodeList = xmlDoc.SelectSingleNode("bookstore"
.ChildNodes
Dim xn As Xml.XmlNode
'遍历所有子结点
For Each xn In nodeList
Dim xe As Xml.XmlElement
xe = xn '将子节点类型转换为XmlElement类型
If xe.GetAttribute("genre"
= "李赞红"
then
'如果genre属性为“李赞红”
xe.SetAttribute("genre", "update李赞红"
'则修改该属性为“update李赞红”
Dim nls As Xml.XmlNodeList
nls = xe.ChildNodes '继续获取xe子节点的所有子节点
Dim xnl As Xml.XmlNode
For Each xnl In nls '遍历
Dim xe2 As Xml.XmlElement
xe2 = xnl '转换类型
If xe2.Name = "author"
then
'如果找到
xe2.InnerText = "亚胜"
'则修改
Exit For '退出循环
End If
Next
Exit For
End If
Next
xmlDoc.Save("../bookstore.xml"
'保存
----结果为:----------------------------------------------------------------------------------------------------
<?xml version="1.0"
encoding="gb2312"?>
<bookstore>
<book genre="fantasy"
ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="update李赞红"
ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>58.3</price>
</book>
</bookstore>
3、删除 <book genre="fantasy"
ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红"
ISBN="2-3631-4">节点。
Dim xmlDoc As Xml.XmlDocument = New Xml.XmlDocument
xmlDoc.Load("../bookstore.xml"
Dim nodeList As Xml.XmlNodeList
'获取bookstore结点的所有子结点
nodeList = xmlDoc.SelectSingleNode("bookstore"
.ChildNodes
Dim xn As Xml.XmlNode
'遍历所有子结点
For Each xn In nodeList
Dim xe As Xml.XmlElement
xe = xn
If xe.GetAttribute("genre"
= "fantasy"
then
xe.RemoveAttribute("genre"
'删除genre属性
else
If xe.GetAttribute("genre"
= "update李赞红"
then
xe.RemoveAll() '删除该节点的全部内容
End If
Next
xmlDoc.Save("../bookstore.xml"
----结果为:----------------------------------------------------------------------------------------------------------
<?xml version="1.0"
encoding="gb2312"?>
<bookstore>
<book ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>
4、显示所有数据
Dim xmlDoc As New Xml.XmlDocument
xmlDoc.Load("../bookstore.xml"
Dim xn As Xml.XmlNode
xn = xmlDoc.SelectSingleNode("bookstore"
Dim xnl As Xml.XmlNodeList
xnl = xn.ChildNodes
Dim xnf As Xml.XmlNode
For Each xnf In xnl
Dim xe As Xml.XmlElement
xe = xnf
txtbook.Text &= xe.GetAttribute("genre"
&
vbCrLf 'Console.WriteLine(xe.GetAttribute("genre"
)
txtbook.Text &= xe.GetAttribute("ISBN"
&
vbCrLf 'Console.WriteLine(xe.GetAttribute("ISBN"
)
Dim xnfl As Xml.XmlNodeList
xnfl = xe.ChildNodes
Dim xn2 As Xml.XmlNode
For Each xn2 In xnfl
txtbook.Text &= xn2.InnerText &
vbCrLf 'Console.WriteLine(xn2.InnerText)
Next
Next
5、读取单个数据
Dim xmlDoc As New Xml.XmlDocument
MessageBox.Show(Application.ExecutablePath)
xmlDoc.Load("../bookstore.xml"
'(Application.ExecutablePath + "/../bookstore.xml"
Dim tmpNode As Xml.XmlNode
Try
tmpNode = xmlDoc.Item("bookstore"
.ChildNodes(0)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
MessageBox.Show(tmpNode.Name)
If tmpNode.Name = "book"
then
MessageBox.Show(tmpNode.ChildNodes(0).InnerText)
MessageBox.Show(tmpNode.ChildNodes(1).InnerText)
MessageBox.Show(tmpNode.ChildNodes(2).InnerText)
End If