VB.NET XMLDocument Method Question

liunx

Guest
I have the following code that parses an XML doc into an ASP.NET page using a VB.NET Function:
<script runat="server">
'Pull in eAlerts data
Function Page_Load
If Not Page.IsPostBack then

Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
'Create the XML Document
m_xmld = New XmlDocument()
'Load the Xml file
m_xmld.Load("http://SERVER/DIRECTORY/docs/xml/ealerts.xml")
'Get the list of name nodes
m_nodelist = m_xmld.SelectNodes("/ealerts/alert")
'Loop through the nodes
For Each m_node In m_nodelist

Dim titleValue = m_node.ChildNodes.Item(0).InnerText
Dim rss_urlValue = m_node.ChildNodes.Item(1).InnerText
Dim linkValue = m_node.ChildNodes.Item(2).InnerText
Dim image_urlValue = m_node.ChildNodes.Item(3).InnerText
Dim widthValue = m_node.ChildNodes.Item(4).InnerText
Dim heightValue = m_node.ChildNodes.Item(5).InnerText
Dim statusValue = m_node.ChildNodes.Item(6).InnerText

'Get the Gender Attribute Value
'Dim genderAttribute = m_node.Attributes.GetNamedItem("gender").Value

'Write TEXT results to the page
'Response.Write("<strong>eAlert</strong><br />" & _
'"Title: " & titleValue & "<br />" & _
'" RSS URL: " & rss_urlValue & "<br />" & _
'" Link: " & linkValue & "<br />" & _
'" Image URL: " & image_urlValue & "<br />" & _
'" Width: " & widthValue & "<br />" & _
'" Height: " & heightValue & "<br />" & _
'" Status: " & statusValue & "<br />")

'Write GRAPHIC results to the page
Response.Write("<a href='http://www.webdeveloper.com/forum/archive/index.php/" & linkValue & "'><img title='" & _
titleValue & "' width='" & widthValue & "' height='" & heightValue & _
"' alt='" & titleValue & "' src='http://www.webdeveloper.com/forum/archive/index.php/" & image_urlValue & "' /></a>")

Next
End If
End Function</script>
...and here's a snippet from the XML doc:
<ealerts>
<alert>
<title>ALERT #1</title>
<rss_url>#</rss_url>
<link>#</link>
<image_url>http://SERVER/DIRECTORY/images/gif/alert1.gif</image_url>
<width>49</width>
<height>48</height>
<status>active</status>
</alert>
<alert>
<title>ALERT #2</title>
<rss_url>#</rss_url>
<link>#</link>
<image_url>http://SERVER/DIRECTORY/images/gif/alert2.gif</image_url>
<width>49</width>
<height>48</height>
<status>inactive</status>
</alert>
</ealerts>

...and this works wonderfully. But I'd like to pull the XML node values with the name of the node instead of the position of the node from the element as it works currently, like this:
FROM:
Dim titleValue = m_node.ChildNodes.Item(0).InnerText
Dim rss_urlValue = m_node.ChildNodes.Item(1).InnerText
...
TO:
Dim titleValue = m_node.ChildNodes.Item("title").InnerText
Dim rss_urlValue = m_node.ChildNodes.Item("rss_url").InnerText
...

Being that I'm a newbie to this process, I'm not sure how to accomplish this yet. If anyone could forward me the syntax that would make this possible, or forward me to where I could find that information, that would be great. Thanks for any & all help.I answered my own questions...gotta love it when that happens.

I just replaced this:
Dim titleValue = m_node.ChildNodes.Item(0).InnerText
Dim rss_urlValue = m_node.ChildNodes.Item(1).InnerText

with this:
Dim titleValue = m_node.Item("title").InnerText
Dim rss_urlValue = m_node.Item("rss_url").InnerText

...and it worked great. I'm really starting to like this XMLDocument method:) Thanks anyway.
 
Back
Top