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

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

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

XmlNode.Attributes

  
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


XmlNode.ChildNodes

  
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