VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • vb.net与XML之间的操作

Imports System.Xml '声明XML
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dataset As New DataSet '声明一个dataset
        dataset.ReadXml("f:\aaa.xml") '读xml文件,并把xml文件放入到datset的第一个表中
        Dim table As DataTable = dataset.Tables(0) '读出该表
        Dim view As DataView = New DataView(table) '自定义视图
        DataGridView1.DataSource = view '绑定数据源
    End Sub
    Public Sub appear() '同上
        Dim dataset As New DataSet
        dataset.ReadXml("f:\aaa.xml")
        Dim table As DataTable = dataset.Tables(0)
        Dim view As DataView = New DataView(table)
        DataGridView2.DataSource = view
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim xmlDou As XmlDocument = New XmlDocument() '声明一个xml文档对象
        xmlDou.Load("f:\aaa.xml") '将特定的xml文档写入xmldoucument
        Dim xmlnode As XmlNode = xmlDou.SelectSingleNode("bookstore") '声明一个根标记,bookstore为根标记
        Dim xml1 As XmlElement = xmlDou.CreateElement("book") '声明一个xml的节点
        xml1.SetAttribute("genre", "李簪红") '节点的属性
        xml1.SetAttribute("ISBN", "2-3621-4") '同上
        Dim xml1_title As XmlElement = xmlDou.CreateElement("title") '声明一个xml的节点
        xml1_title.InnerText = "C#程序设计入门与技巧" '定义节点的文档
        xml1.AppendChild(xml1_title) '向总节点尾部添加节点
        Dim xml1_author As XmlElement = xmlDou.CreateElement("author") '同上
        xml1_author.InnerText = "张永斌"
        xml1.AppendChild(xml1_author)
        Dim xml1_price As XmlElement = xmlDou.CreateElement("price")
        xml1_price.InnerText = "56.7"
        xml1.AppendChild(xml1_price)
        xmlnode.AppendChild(xml1)
        Dim xml2 As XmlElement = xmlDou.CreateElement("book")
        xml2.SetAttribute("genre", "乐于")
        xml2.SetAttribute("ISBN", "3-3423-5")
        Dim xml2_title As XmlElement = xmlDou.CreateElement("title")
        xml2_title.InnerText = "JAVA"
        xml2.AppendChild(xml2_title)
        Dim xml2_author As XmlElement = xmlDou.CreateElement("author")
        xml2_author.InnerText = "防御忧郁"
        xml2.AppendChild(xml2_author)
        Dim xml2_price As XmlElement = xmlDou.CreateElement("price")
        xml2_price.InnerText = "44.4"
        xml2.AppendChild(xml2_price)
        xmlnode.AppendChild(xml2)
        xmlDou.Save("f:\aaa.xml")
        appear()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim xmlDoc As XmlDocument = New XmlDocument '同上
        xmlDoc.Load("f:\aaa.xml") '同上
        Dim nodelist As XmlNodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes() '定义了一个xmlnodelist 用于放根节点为bookstore的所有字标记
        For Each node As XmlNode In nodelist '遍历整个xmlnodelist
            Dim elem As XmlElement = node '将节点转换为element元素
            Dim elem_children As XmlNodeList = elem.ChildNodes() '定义了一个elem_children 用于放根节点为bookstore的所有字标记
            If elem.GetAttribute("genre") = "李簪红" Then '获得第一子节点的属性
                elem.SetAttribute("genre", "updata李簪红") '更新第一节点的属性
            End If
            For Each elem_node As XmlNode In elem_children '遍历第二子节点
                Dim elem_node_toelem As XmlElement = elem_node '将节点转换为element元素
                If elem_node_toelem.Name = "author" Then '如果第二子节点的名称为author
                    If elem_node_toelem.InnerText = "防御忧郁" Then '如果这个节点的值为防御忧郁则
                        elem_node_toelem.InnerText = "O(∩_∩)O哈哈~" '更改他的值
                    End If
                End If
            Next
        Next
        xmlDoc.Save("f:\aaa.xml") '保存xml文件
        appear()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim xmlDuc As XmlDocument = New XmlDocument()
        xmlDuc.Load("f:\aaa.xml")
        Dim xml_geng As XmlElement = xmlDuc.SelectSingleNode("bookstore")
        Dim xmlnodelist As XmlNodeList = xml_geng.ChildNodes()
        For Each xmlnode As XmlNode In xmlnodelist
            Dim xmlnodetoelem As XmlElement = xmlnode
            If xmlnodetoelem.GetAttribute("genre") = "乐于" Then
                xmlnodetoelem.RemoveAttribute("genre")
            End If
        Next
        For Each xmlnode As XmlNode In xmlnodelist
            Dim xmlnodetoelem As XmlElement = xmlnode
            If xmlnodetoelem.GetAttribute("genre") = "fantasy" Then
                xmlnodetoelem.RemoveAll()
            End If
        Next
        xmlDuc.Save("f:\aaa.xml") '保存xml文件
        appear()
    End Sub
End Class


相关教程