Reading multiple nodes with the same name

knee_boarder

Member
Joined
Oct 5, 2009
Messages
5
Location
United Kingdom
Programming Experience
5-10
I'm trying to get the values from xml produced by an rss feed which I feed into an XmlDocument.

example xml

VB.NET:
<item>
<title>
Tennis courts are undergoing improvements this autumn.
</title>
<link>
link
</link>
<comments>
comments
</comments>
<pubDate>Thu, 26 Sep 2013 13:08:12 +0000</pubDate>
<dc:creator>Mair</dc:creator>
<category>
<![CDATA[ Homepage Thumbnail ]]>
</category>
<category>
<![CDATA[ Homepage Top Stories ]]>
</category>
<category>
<![CDATA[ Leisure ]]>
</category>
<category>
<![CDATA[ Thumbnail news ]]>
</category>
<category>
<![CDATA[ Top Stories ]]>
</category>
<category>
<![CDATA[ Football ]]>
</category>
<category>
<![CDATA[ ilfracombe ]]>
</category>
<description>
<![CDATA[
Tennis courts are undergoing improvements this autumn.
]]>
</description>
</item>

I have no problem in reading out the first category using this code

VB.NET:
Dim rssXmlDoc As New XmlDocument

        ' Load the RSS file from the RSS URL
        rssXmlDoc.Load(strUrl)

        ' Parse the Items in the RSS file
        Dim rssNodes As XmlNodeList = rssXmlDoc.SelectNodes("rss/channel/item")

        Dim title As String
        Dim link As String
        Dim category As String

        Dim rssNode As XmlNode
        Dim rssSubNode As XmlNode

        'Iterate through the items in the RSS file
        For Each rssNode In rssNodes

            rssSubNode = rssNode.SelectSingleNode("title")
            title = rssSubNode.InnerText

            rssSubNode = rssNode.SelectSingleNode("link")
            link = rssSubNode.InnerText

            rssSubNode = rssNode.SelectSingleNode("category")
            category = rssSubNode.InnerText

            Response.Write("<a href=""" + link + """>" + title + "</a>" + "<br /><br />")

            Response.Write(category + "<br /><br />")

but I can't figure out how to iterate through all the <category>'s. There can be any number of category's added so it's not a fixed quantity.
 
Hi,

I am surprised that you did not work this out for yourself. All you need to do is to iterate another SelectNodes Collection rather than calling SelectSingleNode. i.e:-

For Each itemNode As XmlNode In myXMLDoc.DocumentElement.SelectNodes("channel/item")
  MsgBox(itemNode.SelectSingleNode("title").InnerText)
  'etc etc...
  For Each categoryNode As XmlNode In itemNode.SelectNodes("category")
    MsgBox(categoryNode.InnerText)
  Next
Next


Hope that helps.

Cheers,

Ian
 
Back
Top