订阅专栏
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
增加一个Book节点数据,为了简化代码,这里直接写数据,不接受别的数据
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim rootElement As XmlElement
rootElement = xmlDoc.DocumentElement
Dim xmlNewBook As XmlElement
xmlNewBook = xmlDoc.CreateElement("Book")
xmlNewBook.SetAttribute("ISBN", "1234567890")
rootElement.AppendChild(xmlNewBook)
Dim xmlNewEle As XmlElement
xmlNewEle = xmlDoc.CreateElement("TITLE")
xmlNewEle.InnerText = "星球大战:新的历史"
xmlNewBook.AppendChild(xmlNewEle)
xmlNewEle = xmlDoc.CreateElement("AUTHOR")
xmlNewEle.InnerText = "迪斯尼"
xmlNewBook.AppendChild(xmlNewEle)
xmlNewEle = xmlDoc.CreateElement("COUNTRY")
xmlNewEle.InnerText = "美国"
xmlNewBook.AppendChild(xmlNewEle)
xmlNewEle = xmlDoc.CreateElement("PRICE")
xmlNewEle.InnerText = "10.00"
xmlNewBook.AppendChild(xmlNewEle)
xmlNewEle = xmlDoc.CreateElement("YEAR")
xmlNewEle.InnerText = "https://files.jxasp.com/image/2018"
xmlNewBook.AppendChild(xmlNewEle)
Call showXML()
End Sub
通过 rootElement = xmlDoc.DocumentElement 获得根节点
定义一个xml元素:xmlNewBook
使xmlNewBook为xmlDoc创建的Book元素:xmlDoc.CreateElement("Book")
设置属性:xmlNewBook.SetAttribute("ISBN", "1234567890")
将元素添加到根节点下:rootElement.AppendChild(xmlNewBook)
注意,xml元素也是xml的节点,在.net中,XmlElement继承于XmlNode
同样,在xmlDoc中创建元素,并添加到xmlNewBook节点下。
运行结果:
xml中节点的删除:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If ListView1.SelectedItems.Count < 1 Then Exit Sub
Dim lvItem As ListViewItem
lvItem = ListView1.SelectedItems(0)
If lvItem Is Nothing Then
Exit Sub
End If
Dim rootElement As XmlElement
rootElement = xmlDoc.DocumentElement
Dim isbn As String = lvItem.Text
Dim condition As String = "/BookStore/Book[@ISBN='" & isbn & "']"
Dim xmlNd As XmlNode = xmlDoc.SelectSingleNode(condition)
'xmlNd.RemoveAll()
rootElement.RemoveChild(xmlNd)
Call showXML()
End Sub
代码中,condition是一个XPath字符串,"/BookStore/Book[@ISBN='" & isbn & "']" 是需要查询的BookStore节点下属性ISBN为isbn的Book节点。
关于XPath相关知识,请自行网上搜索。
使用xmDoc.SelectSingleNode()执行查询。
查询出来的节点使用根节点的Remove方法进行移除。
大家可以看看将xmlNd.RemoveAll() 前的单引号取消,将 rootElement.RemoveChild(xmlNd) 注释掉,再运行看看会有什么结果。
节点中数据的修改:
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If ListView1.SelectedItems.Count < 1 Then Exit Sub
Dim lvItem As ListViewItem
lvItem = ListView1.SelectedItems(0)
If lvItem Is Nothing Then
Exit Sub
End If
Dim rootElement As XmlElement
rootElement = xmlDoc.DocumentElement
Dim isbn As String = lvItem.Text
Dim condition As String = "/BookStore/Book[@ISBN='" & isbn & "']"
Dim xmlNd As XmlNode = xmlDoc.SelectSingleNode(condition)
xmlNd.Attributes("ISBN").Value = "9876543210"
xmlNd.ChildNodes(0).InnerText = "星球大战:新的篇章"
xmlNd.ChildNodes(1).InnerText = "乔治·卢卡斯"
xmlNd.ChildNodes(2).InnerText = "美国"
xmlNd.ChildNodes(3).InnerText = "50.00"
xmlNd.ChildNodes(4).InnerText = "https://files.jxasp.com/image/2018"
Call showXML()
End Sub
设置选择的Book节点ISBN属性:xmlNd.Attributes("ISBN").Value = "9876543210"
设置节点下元素的值:xmlNd.ChildNodes(0).InnerText="星球大战:新的篇章"
运行结果:
不管增加、删除还是修改,最后都别忘了保存数据:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
xmlDoc.Save("d:\books.xml")
End Sub
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net教程目录
————————————————
版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。