Partial Class GetElementsByTagName
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim xmlFile As String = Server.MapPath("DvdList.xml")
Dim doc As New XmlDocument()
doc.Load(xmlFile)
'find all the
Dim str As New StringBuilder()
Dim nodes As XmlNodeList = doc.GetElementsByTagName("Title")
For Each node As XmlNode In nodes
str.Append("Found: ")
'show the text contained in this
str.Append(node.ChildNodes(0).Value)
str.Append("
")
Next
Response.Write(str.ToString())
'clear the string builder
str.Length = 0
For Each node As XmlNode In nodes
str.Append("Found: <b>")
'show the text contained in this
Dim name As String = node.ChildNodes(0).Value
str.Append(name)
str.Append("</b><br />")
If name = "Forrest Gump" Then
'find the stars for just this movie
'first you need to get the parent node
'which is the <DVD> element for the movie
Dim parent As XmlNode = node.ParentNode
'Then you need to search down the tree
Dim childNodes As XmlNodeList = (CType(parent, XmlElement)).GetElementsByTagName("Star")
For Each childNode As XmlNode In childNodes
str.Append(" Found Star: ")
str.Append(childNode.ChildNodes(0).Value)
str.Append("
")
Next
End If
Next
Response.Write("<hr> <br /> <br/>")
Response.Write(str.ToString())
End Sub
End Class
No comments:
Post a Comment