VB.Net by API/System.Xml/XmlDocument — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:50, 26 мая 2010

XmlDocument.AppendChild

  

public class DOMSave
   public Shared Sub Main
        Dim myDoc As New Xml.XmlDocument
        myDoc.AppendChild(myDoc.CreateXmlDeclaration("1.0", "UTF-8", String.Empty))
        myDoc.AppendChild(myDoc.CreateComment("information about this book"))
        Dim rootBook As Xml.XmlNode =myDoc.AppendChild(myDoc.CreateElement("book"))
        rootBook.Attributes.Append(myDoc.CreateAttribute("isbn"))
        rootBook.Attributes("isbn").Value = "0-672-32549-7"
        Dim title As Xml.XmlNode
        title = rootBook.AppendChild(myDoc.CreateElement("title"))
        title.InnerText = "Start"
        Dim authors As Xml.XmlNode = rootBook.AppendChild(myDoc.CreateElement("authors"))
        Dim Duncan, Andy, Erik, Joel As Xml.XmlNode
        Duncan = authors.AppendChild(myDoc.CreateElement("author"))
        Andy = authors.AppendChild(myDoc.CreateElement("author"))
        Erik = authors.AppendChild(myDoc.CreateElement("author"))
        Joel = authors.AppendChild(myDoc.CreateElement("author"))
        Duncan.InnerText = "D"
        Andy.InnerText = "A"
        Erik.InnerText = "E"
        Joel.InnerText = "J"
        Dim chapters As Xml.XmlNode = myDoc.CreateElement("chapters")
        rootBook.AppendChild(chapters)
        Dim chapterElements(10) As Xml.XmlElement
        For i As Integer = 0 To 10
            chapterElements(i) = myDoc.CreateElement("chapter")
            With chapterElements(i)
                .Attributes.Append(myDoc.CreateAttribute("id"))
                .Attributes("id").Value =CStr(i + 1)
                .Attributes.Append(myDoc.CreateAttribute("topic"))
            End With
            chapters.AppendChild(chapterElements(i))
        Next

        chapterElements(0).Attributes("topic").Value = "A"
        chapterElements(1).Attributes("topic").Value = "B"
        chapterElements(2).Attributes("topic").Value = "C"
        chapterElements(3).Attributes("topic").Value = "D"
        chapterElements(4).Attributes("topic").Value = "E"
        chapterElements(5).Attributes("topic").Value = "F"
        chapterElements(6).Attributes("topic").Value = "G"
        chapterElements(7).Attributes("topic").Value = "H"
        chapterElements(8).Attributes("topic").Value = "I"
        chapterElements(9).Attributes("topic").Value = "J"
        chapterElements(10).Attributes("id").Value = "K"
        chapterElements(10).Attributes("topic").Value = "L"
        Dim myWriter As New Xml.XmlTextWriter("test.xml", System.Text.Encoding.UTF8)
        myWriter.Formatting = Xml.Formatting.Indented
        myDoc.Save(myWriter)
        myWriter.Close()
   End Sub
End class


XmlDocument.CreateAttribute

  

public class DOMSave
   public Shared Sub Main
        Dim myDoc As New Xml.XmlDocument
        myDoc.AppendChild(myDoc.CreateXmlDeclaration("1.0", "UTF-8", String.Empty))
        myDoc.AppendChild(myDoc.CreateComment("information about this book"))
        Dim rootBook As Xml.XmlNode =myDoc.AppendChild(myDoc.CreateElement("book"))
        rootBook.Attributes.Append(myDoc.CreateAttribute("isbn"))
        rootBook.Attributes("isbn").Value = "0-672-32549-7"
        Dim title As Xml.XmlNode
        title = rootBook.AppendChild(myDoc.CreateElement("title"))
        title.InnerText = "Start"
        Dim authors As Xml.XmlNode = rootBook.AppendChild(myDoc.CreateElement("authors"))
        Dim Duncan, Andy, Erik, Joel As Xml.XmlNode
        Duncan = authors.AppendChild(myDoc.CreateElement("author"))
        Andy = authors.AppendChild(myDoc.CreateElement("author"))
        Erik = authors.AppendChild(myDoc.CreateElement("author"))
        Joel = authors.AppendChild(myDoc.CreateElement("author"))
        Duncan.InnerText = "D"
        Andy.InnerText = "A"
        Erik.InnerText = "E"
        Joel.InnerText = "J"
        Dim chapters As Xml.XmlNode = myDoc.CreateElement("chapters")
        rootBook.AppendChild(chapters)
        Dim chapterElements(10) As Xml.XmlElement
        For i As Integer = 0 To 10
            chapterElements(i) = myDoc.CreateElement("chapter")
            With chapterElements(i)
                .Attributes.Append(myDoc.CreateAttribute("id"))
                .Attributes("id").Value =CStr(i + 1)
                .Attributes.Append(myDoc.CreateAttribute("topic"))
            End With
            chapters.AppendChild(chapterElements(i))
        Next

        chapterElements(0).Attributes("topic").Value = "A"
        chapterElements(1).Attributes("topic").Value = "B"
        chapterElements(2).Attributes("topic").Value = "C"
        chapterElements(3).Attributes("topic").Value = "D"
        chapterElements(4).Attributes("topic").Value = "E"
        chapterElements(5).Attributes("topic").Value = "F"
        chapterElements(6).Attributes("topic").Value = "G"
        chapterElements(7).Attributes("topic").Value = "H"
        chapterElements(8).Attributes("topic").Value = "I"
        chapterElements(9).Attributes("topic").Value = "J"
        chapterElements(10).Attributes("id").Value = "K"
        chapterElements(10).Attributes("topic").Value = "L"
        Dim myWriter As New Xml.XmlTextWriter("test.xml", System.Text.Encoding.UTF8)
        myWriter.Formatting = Xml.Formatting.Indented
        myDoc.Save(myWriter)
        myWriter.Close()
   End Sub
End class


XmlDocument.CreateComment

  

public class DOMSave
   public Shared Sub Main
        Dim myDoc As New Xml.XmlDocument
        myDoc.AppendChild(myDoc.CreateXmlDeclaration("1.0", "UTF-8", String.Empty))
        myDoc.AppendChild(myDoc.CreateComment("information about this book"))
        Dim rootBook As Xml.XmlNode =myDoc.AppendChild(myDoc.CreateElement("book"))
        rootBook.Attributes.Append(myDoc.CreateAttribute("isbn"))
        rootBook.Attributes("isbn").Value = "0-672-32549-7"
        Dim title As Xml.XmlNode
        title = rootBook.AppendChild(myDoc.CreateElement("title"))
        title.InnerText = "Start"
        Dim authors As Xml.XmlNode = rootBook.AppendChild(myDoc.CreateElement("authors"))
        Dim Duncan, Andy, Erik, Joel As Xml.XmlNode
        Duncan = authors.AppendChild(myDoc.CreateElement("author"))
        Andy = authors.AppendChild(myDoc.CreateElement("author"))
        Erik = authors.AppendChild(myDoc.CreateElement("author"))
        Joel = authors.AppendChild(myDoc.CreateElement("author"))
        Duncan.InnerText = "D"
        Andy.InnerText = "A"
        Erik.InnerText = "E"
        Joel.InnerText = "J"
        Dim chapters As Xml.XmlNode = myDoc.CreateElement("chapters")
        rootBook.AppendChild(chapters)
        Dim chapterElements(10) As Xml.XmlElement
        For i As Integer = 0 To 10
            chapterElements(i) = myDoc.CreateElement("chapter")
            With chapterElements(i)
                .Attributes.Append(myDoc.CreateAttribute("id"))
                .Attributes("id").Value =CStr(i + 1)
                .Attributes.Append(myDoc.CreateAttribute("topic"))
            End With
            chapters.AppendChild(chapterElements(i))
        Next

        chapterElements(0).Attributes("topic").Value = "A"
        chapterElements(1).Attributes("topic").Value = "B"
        chapterElements(2).Attributes("topic").Value = "C"
        chapterElements(3).Attributes("topic").Value = "D"
        chapterElements(4).Attributes("topic").Value = "E"
        chapterElements(5).Attributes("topic").Value = "F"
        chapterElements(6).Attributes("topic").Value = "G"
        chapterElements(7).Attributes("topic").Value = "H"
        chapterElements(8).Attributes("topic").Value = "I"
        chapterElements(9).Attributes("topic").Value = "J"
        chapterElements(10).Attributes("id").Value = "K"
        chapterElements(10).Attributes("topic").Value = "L"
        Dim myWriter As New Xml.XmlTextWriter("test.xml", System.Text.Encoding.UTF8)
        myWriter.Formatting = Xml.Formatting.Indented
        myDoc.Save(myWriter)
        myWriter.Close()
   End Sub
End class


XmlDocument.CreateElement

  


Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Imports System.Data
Imports System.Windows.Forms

Public Class MainClass
    
    Shared Sub Main()
        Dim FakeQuantity As Integer
        Dim Doc As New XmlDocument()
        Dim newAtt As XmlAttribute
        Dim TempNode As XmlElement
        Dim dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", _
                                         Nothing, Nothing)
        Doc.AppendChild(dec)
        Dim DocRoot As XmlElement = Doc.CreateElement("Orders")
        Doc.AppendChild(DocRoot)
        Dim x As Integer
        For x = 0 To 11
            Dim Order As XmlNode = Doc.CreateElement("Order")
            newAtt = Doc.CreateAttribute("Quantity")
            FakeQuantity = 10 * x + x
            newAtt.Value = FakeQuantity.ToString()
            Order.Attributes.Append(newAtt)
            DocRoot.AppendChild(Order)
        Next
        Doc.Save("OutDocument.xml")
    End Sub
End Class


XmlDocument.CreateNavigator

  
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl
Imports System.IO
Public Class MainClass
   Shared Sub Main()
       Dim document As XmlDocument     " Xml document root
       Dim navigator As XPathNavigator " navigate document
       Dim transformer As XslTransform " transform document
       Dim output As StringWriter
       
      document = New XmlDocument()
      document.Load("games.xml")
      navigator = document.CreateNavigator
      transformer = New XslTransform()
      transformer.Load("games.xsl")
      output = New StringWriter()
      transformer.Transform(navigator, Nothing, output)
      Console.WriteLine(output.ToString)
      output.Close()
   End Sub
End Class


XmlDocument.CreateXmlDeclaration

  


public class DOMSave
   public Shared Sub Main
        Dim myDoc As New Xml.XmlDocument
        myDoc.AppendChild(myDoc.CreateXmlDeclaration("1.0", "UTF-8", String.Empty))
        myDoc.AppendChild(myDoc.CreateComment("information about this book"))
        Dim rootBook As Xml.XmlNode =myDoc.AppendChild(myDoc.CreateElement("book"))
        rootBook.Attributes.Append(myDoc.CreateAttribute("isbn"))
        rootBook.Attributes("isbn").Value = "0-672-32549-7"
        Dim title As Xml.XmlNode
        title = rootBook.AppendChild(myDoc.CreateElement("title"))
        title.InnerText = "Start"
        Dim authors As Xml.XmlNode = rootBook.AppendChild(myDoc.CreateElement("authors"))
        Dim Duncan, Andy, Erik, Joel As Xml.XmlNode
        Duncan = authors.AppendChild(myDoc.CreateElement("author"))
        Andy = authors.AppendChild(myDoc.CreateElement("author"))
        Erik = authors.AppendChild(myDoc.CreateElement("author"))
        Joel = authors.AppendChild(myDoc.CreateElement("author"))
        Duncan.InnerText = "D"
        Andy.InnerText = "A"
        Erik.InnerText = "E"
        Joel.InnerText = "J"
        Dim chapters As Xml.XmlNode = myDoc.CreateElement("chapters")
        rootBook.AppendChild(chapters)
        Dim chapterElements(10) As Xml.XmlElement
        For i As Integer = 0 To 10
            chapterElements(i) = myDoc.CreateElement("chapter")
            With chapterElements(i)
                .Attributes.Append(myDoc.CreateAttribute("id"))
                .Attributes("id").Value =CStr(i + 1)
                .Attributes.Append(myDoc.CreateAttribute("topic"))
            End With
            chapters.AppendChild(chapterElements(i))
        Next

        chapterElements(0).Attributes("topic").Value = "A"
        chapterElements(1).Attributes("topic").Value = "B"
        chapterElements(2).Attributes("topic").Value = "C"
        chapterElements(3).Attributes("topic").Value = "D"
        chapterElements(4).Attributes("topic").Value = "E"
        chapterElements(5).Attributes("topic").Value = "F"
        chapterElements(6).Attributes("topic").Value = "G"
        chapterElements(7).Attributes("topic").Value = "H"
        chapterElements(8).Attributes("topic").Value = "I"
        chapterElements(9).Attributes("topic").Value = "J"
        chapterElements(10).Attributes("id").Value = "K"
        chapterElements(10).Attributes("topic").Value = "L"
        Dim myWriter As New Xml.XmlTextWriter("test.xml", System.Text.Encoding.UTF8)
        myWriter.Formatting = Xml.Formatting.Indented
        myDoc.Save(myWriter)
        myWriter.Close()
   End Sub
End class


XmlDocument.GetElementsByTagName

  
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Public Class MainClass
   Public Shared Sub Main()
        Dim rawData As String = _
            "<Products>" & _
            "  <Product>" & _
            "    <name>Name 1</name>" & _
            "    <Id>101</Id>" & _
            "    <quantity>10</quantity>" & _
            "  </Product>" & _
            "  <Product>" & _
            "    <name>Name 2</name>" & _
            "    <Id>102</Id>" & _
            "    <quantity>10</quantity>" & _
            "  </Product>" & _
            "</Products>"
        Dim xmlDoc As New XmlDocument
        Dim productNodes As XmlNodeList
        Dim productNode As XmlNode
        Dim baseDataNodes As XmlNodeList
        Dim bFirstInRow As Boolean
        xmlDoc.LoadXml(rawData)
        productNodes = xmlDoc.GetElementsByTagName("Product")
        For Each productNode In productNodes
            baseDataNodes = productNode.ChildNodes
            bFirstInRow = True
            For Each baseDataNode As XmlNode In baseDataNodes
                If (bFirstInRow) Then
                    bFirstInRow = False
                Else
                    Console.Write(", ")
                End If
                Console.Write(baseDataNode.Name & ": " & baseDataNode.InnerText)
            Next
        Next
   End Sub
End Class


XmlDocument.LoadXml

  
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Public Class MainClass
   Public Shared Sub Main()
        Dim rawData As String = _
            "<Products>" & _
            "  <Product>" & _
            "    <name>Name 1</name>" & _
            "    <Id>101</Id>" & _
            "    <quantity>10</quantity>" & _
            "  </Product>" & _
            "  <Product>" & _
            "    <name>Name 2</name>" & _
            "    <Id>102</Id>" & _
            "    <quantity>10</quantity>" & _
            "  </Product>" & _
            "</Products>"
        Dim xmlDoc As New XmlDocument
        Dim productNodes As XmlNodeList
        Dim productNode As XmlNode
        Dim baseDataNodes As XmlNodeList
        Dim bFirstInRow As Boolean
        xmlDoc.LoadXml(rawData)
        productNodes = xmlDoc.GetElementsByTagName("Product")
        For Each productNode In productNodes
            baseDataNodes = productNode.ChildNodes
            bFirstInRow = True
            For Each baseDataNode As XmlNode In baseDataNodes
                If (bFirstInRow) Then
                    bFirstInRow = False
                Else
                    Console.Write(", ")
                End If
                Console.Write(baseDataNode.Name & ": " & baseDataNode.InnerText)
            Next
        Next
   End Sub
End Class


XmlDocument.SelectNodes

  

public class Test
   public Shared Sub Main
        Dim myDoc As New Xml.XmlDocument
        myDoc.Load("test.xml")
        Dim chpts As Xml.XmlNodeList = myDoc.SelectNodes("/book/chapters/chapter")
        For Each nd As Xml.XmlNode In chpts
            Console.WriteLine(nd.Attributes("id").Value)
        Next

        Dim book As Xml.XmlElement
        book = myDoc.Item("book")
        Console.WriteLine("ISBN: " & book.Attributes("isbn").Value)
        Dim title As Xml.XmlElement
        title = book.Item("title")
        Console.WriteLine(title.InnerText)
        Console.WriteLine("Authors:")
        Dim authors As Xml.XmlElement
        authors = book.Item("authors")
        For Each node As Xml.XmlNode In authors.ChildNodes
            If node.Name = "author" Then
                Console.WriteLine(node.InnerText)
            End If
        Next
        Dim chapters As Xml.XmlElement
        chapters = book.Item("chapters")
        For Each node As Xml.XmlNode In chapters.ChildNodes
            If node.Name = "chapter" Then
                Console.WriteLine(node.Attributes("id").Value)
                Console.WriteLine(node.Attributes("topic").Value)
            End If
        Next
   End Sub
End class