VB.Net by API/System.Xml/XmlElement — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:50, 26 мая 2010
XmlElement.ChildNodes
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
XmlElement.InnerText
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
XmlElement.Item
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