VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • 转:XML的几种读取,修改方式(VB.NET)

1,读取XML 
Imports System.Xml
Imports System.Web.HttpContext
  Public Sub getPro()
        Dim FilePath As String = Current.Server.MapPath("define_auth.xml")
        Dim re As XmlTextReader = New XmlTextReader(FilePath)
        Dim name As String
        Try
            While re.Read
                Select Case re.NodeType
                    Case XmlNodeType.Element
                        name = re.Name
                    Case XmlNodeType.Text
                        setProperty(name, re.Value)
                End Select
            End While
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    'name      :XML Node的名字
    'Nodetext  :XML Node的值
    Private Sub setProperty(ByVal NodeName As String, ByVal NodeValue As String)
        Select Case NodeName
            Case "provider"
                sqlProvider = NodeValue
            Case "connectionstring"
                connectionStr = NodeValue
        End Select
    End Sub

2,下面是转载别人的部分,重写成了VB.NET
    Dim xmldoc As XmlDocument 
    Dim xmlnode As xmlnode 
    Dim xmlelem As XmlElement
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        xmldoc = New XmlDocument
        '日本语<?xml version="1.0" encoding="shift_jis"?>
        Dim xmldecl As XmlDeclaration
        xmldecl = xmldoc.CreateXmlDeclaration("1.0", "shift_jis", String.Empty)
        xmldoc.AppendChild(xmldecl)
        'add a new root element
        xmlelem = xmldoc.CreateElement("", "Employees", "")
        xmldoc.AppendChild(xmlelem)
        'add other elements
        Dim i As New Integer
        For i = 1 To 3
            Dim root As XmlNode = xmldoc.SelectSingleNode("Employees") 'search <Employees>
            Dim xe1 As XmlElement = xmldoc.CreateElement("Node") 'create <Node>
            xe1.SetAttribute("genre", "Liu")
            xe1.SetAttribute("ISBN", "2-3631-4")
            Dim xesub1 As XmlElement = xmldoc.CreateElement("title")
            xesub1.InnerText = "Book1"
            xe1.AppendChild(xesub1) 'add to <node>
            Dim xesub2 As XmlElement = xmldoc.CreateElement("author")
            xesub2.InnerText = "Hou"
            xe1.AppendChild(xesub2)
            Dim xesub3 As XmlElement = xmldoc.CreateElement("price")
            xesub3.InnerText = "58.3"
            xe1.AppendChild(xesub3)
            root.AppendChild(xe1) 'add to <Employees>
        Next
        xmldoc.Save(Server.MapPath("data.xml"))
    End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim xmlWriter As XmlTextWriter
        Dim strFilename As String = Server.MapPath("data1.xml")
        xmlWriter = New XmlTextWriter(strFilename, System.Text.Encoding.Default)
        xmlWriter.Formatting = Formatting.Indented
        xmlWriter.WriteStartDocument()
        xmlWriter.WriteStartElement("Employees")
        xmlWriter.WriteStartElement("Node")
        xmlWriter.WriteAttributeString("genre", "Li")
        xmlWriter.WriteAttributeString("ISBN", "2-3631-4")
        xmlWriter.WriteStartElement("title")
        xmlWriter.WriteString("Book2")
        xmlWriter.WriteEndElement()
        xmlWriter.WriteStartElement("author")
        xmlWriter.WriteString("Jia")
        xmlWriter.WriteEndElement()
        xmlWriter.WriteStartElement("price")
        xmlWriter.WriteString("58.3")
        xmlWriter.WriteEndElement()
        xmlWriter.WriteEndElement()
        xmlWriter.Close()
    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(Server.MapPath("data.xml"))
        Dim root As XmlNode = xmlDoc.SelectSingleNode("Employees")
        Dim xe1 As XmlElement = xmlDoc.CreateElement("Node")
        xe1.SetAttribute("genre", "張さん")
        xe1.SetAttribute("ISBN", "1-1111-1")
        Dim xesub1 As XmlElement = xmlDoc.CreateElement("title")
        xesub1.InnerText = "New Book"
        xe1.AppendChild(xesub1)
        Dim xesub2 As XmlElement = xmlDoc.CreateElement("author")
        xesub2.InnerText = "H"
        xe1.AppendChild(xesub2)
        Dim xesub3 As XmlElement = xmlDoc.CreateElement("price")
        xesub3.InnerText = "158.3"
        xe1.AppendChild(xesub3)
        root.AppendChild(xe1)
        xmlDoc.Save(Server.MapPath("data.xml"))

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim xmldoc As New XmlDocument
        xmldoc.Load(Server.MapPath("data.xml"))
        Dim nodeList As XmlNodeList = xmldoc.SelectSingleNode("Employees").ChildNodes
        For Each xn As XmlNode In nodeList
            Dim xe As XmlElement
            xe = xn
            'xe =  (XmlElement)xn
            If xe.GetAttribute("genre") = "張さん" Then
                xe.SetAttribute("genre", "update張さん")

                Dim nls As XmlNodeList = xe.ChildNodes
                For Each xn1 As XmlNode In nls
                    Dim xe2 As XmlElement
                    xe2 = xn1
                    'xe2 =(XmlElement)xn1
                    If xe2.Name = "author" Then
                        xe2.InnerText = "量"
                    End If
                Next
            End If
        Next
        xmldoc.Save(Server.MapPath("data.xml"))
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim xmldoc As XmlDocument = New XmlDocument
        xmldoc.Load(Server.MapPath("data.xml"))
        Dim nodeList As XmlNodeList = xmldoc.SelectSingleNode("Employees").ChildNodes
        For Each xn As XmlNode In nodeList
            Dim xe As XmlElement
            xe = xn
            xe.SetAttribute("test", "111111")
            Dim xesub As XmlElement = xmldoc.CreateElement("flag")
            xesub.InnerText = "1"
            xe.AppendChild(xesub)
        Next
        xmldoc.Save(Server.MapPath("data.xml"))
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim xmldoc As XmlDocument = New XmlDocument
        xmldoc.Load(Server.MapPath("data.xml"))
        Dim xnl As XmlNodeList = xmldoc.SelectSingleNode("Employees").ChildNodes
        For Each xn As XmlNode In xnl
            Dim xe As XmlElement
            xe = xn
            xe.RemoveAttribute("genre")
            Dim nls As XmlNodeList = xe.ChildNodes
            For Each xn1 As XmlNode In nls
                Dim xe2 As XmlElement
                xe2 = xn1
                If xe2.Name = "flag" Then
                    xe.RemoveChild(xe2)
                End If
            Next
        Next
        xmldoc.Save(Server.MapPath("data.xml"))
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim xmlDoc As XmlDocument = New XmlDocument
        xmlDoc.Load(Server.MapPath("data.xml"))
        Dim root As XmlNode = xmlDoc.SelectSingleNode("Employees")
        Dim xnl As XmlNodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes
        Dim i As Integer
        For i = 0 To i < xnl.Count
            Dim xe As XmlElement
            xe = xnl.Item( i )
            If xe.GetAttribute("genre") = "張さん" Then
                root.RemoveChild(xe)
                If i < xnl.Count Then i = i - 1
            End If
        Next
        xmlDoc.Save(Server.MapPath("data.xml"))
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Dim myFile As System.IO.StreamReader = _
        New System.IO.StreamReader(Server.MapPath("data.xml"), System.Text.Encoding.Default)
        'Attention System.Text.Encoding.Default
        Dim myString As String = myFile.ReadToEnd() 'myString was read
        myFile.Close()
    End Sub


摘自http://www.cnblogs.com/Ronin/archive/2005/09/13/236006.html

相关教程