VB.Net Tutorial/XML/DOM — различия между версиями

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

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

DOM load

<source lang="vbnet">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</source>

1
2
3
4
5
6
7
8
9
10
K
ISBN: 0-672-32549-7
Start
Authors:
D
A
E
J
1
A
2
B
3
C
4
D
5
E
6
F
7
G
8
H
9
I
10
J
K
L

DOM save

<source lang="vbnet">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</source>