vb.net 教程 4-10 XML文件操作 2

前端之家收集整理的这篇文章主要介绍了vb.net 教程 4-10 XML文件操作 2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

增加一个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 = "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 = "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 教程 目录

原文链接:https://www.f2er.com/vb/256196.html

猜你在找的VB相关文章