vb.net access xml file

Imports System.Xml

Module Module2

    Sub main()
        Dim xmlDoc As New XmlDocument()
        xmlDoc.Load("e:\study\test.xml")
        Dim root As XmlNode = xmlDoc.SelectSingleNode("bookstore")

        'Add node
        'Dim ele1 As XmlElement = xmlDoc.CreateElement("book")
        'ele1.SetAttribute("genre", "Julia chen")
        'ele1.SetAttribute("ISBN", "2-3631-4")
        'Dim ele11 As XmlElement = xmlDoc.CreateElement("title")
        'ele11.InnerText = "visual studio 2005"+
        'ele1.AppendChild(ele11)
        'Dim ele12 As XmlElement = xmlDoc.CreateElement("anthor")
        'ele12.InnerText = "bruce ,aliun"
        'ele1.AppendChild(ele12)
        'Dim ele13 As XmlElement = xmlDoc.CreateElement("price")
        'ele13.InnerText = "15.00"
        'ele1.AppendChild(ele13)
        'root.AppendChild(ele1)

        'Update node
        'Dim nodeList As XmlNodeList = root.ChildNodes
        'Dim ele As XmlNode
        'For Each ele In nodeList
        '    Dim eleE As XmlElement = CType(ele, XmlElement)
        '    If eleE.GetAttribute("genre").Equals("Julia chen") Then
        '        eleE.SetAttribute("ISBN", "2-3631-8")
        '        Dim xm As XmlNode
        '        For Each xm In eleE.ChildNodes
        '            Dim xmll As XmlElement = CType(xm, XmlElement)
        '            If xmll.Name.Equals("anthor") Then
        '                xmll.InnerText = "nicholas"
        '                Exit For
        '            End If
        '        Next
        '        Exit For
        '    End If
        'Next

        'Remove node
        'Dim nodeList As XmlNodeList = root.ChildNodes
        'Dim node As XmlNode
        'For Each node In nodeList
        '    Dim ele As XmlElement = CType(node, XmlElement)
        '    If ele.GetAttribute("genre").Equals("Julia chen") Then
        '        root.RemoveChild(ele)
        '    ElseIf ele.GetAttribute("genre").Equals("fantasy") Then
        '        ele.RemoveAttribute("genre")
        '    End If
        'Next

        'Show all
        Dim nodeList As XmlNodeList = root.ChildNodes
        Dim node As XmlNode
        For Each node In nodeList
            Dim ele As XmlElement = CType(node, XmlElement)
            Console.WriteLine(ele.GetAttribute("genre"))
            Console.WriteLine(ele.GetAttribute("ISBN"))
            Dim subNode As XmlNode
            For Each subNode In ele.ChildNodes
                Console.WriteLine(subNode.InnerText)
            Next
        Next
        Console.ReadLine()

        xmlDoc.Save("e:\study\test.xml")
        'xmlDoc.Save("e:\study\lxj.xml")
    End Sub

End Module


test.xml file:

<?xml version="1.0" encoding="gb2312"?>
<bookstore>
  <book genre="fantasy" ISBN="2-3631-4">
    <title>world legend</title>
    <author>david</author>
    <price>100.0</price>
  </book>
  <book genre="Julia chen" ISBN="2-3631-8">
    <title>visual studio 2005</title>
    <anthor>nicholas</anthor>
    <price>15.00</price>
  </book>
</bookstore>

你可能感兴趣的:(xml,.net,Access,vb,VB.NET)