Solved

ASP.NET/VB: Loop through XML to get more than one result

Posted on 2013-12-17
3
581 Views
Last Modified: 2013-12-17
This only returns the first URL.  I want all URLs to be returned.
<%@ Page Language="VB" %>
<%@Import namespace="System"%>
<%@Import namespace="System.Xml"%>
<%@Import namespace="System.IO"%>
<%@Import namespace="System.Text"%>
<%

Dim XML As String
Using WC As New System.Net.WebClient()
 XML = WC.DownloadString("http://www.freesitemapgenerator.com/sitemap.xml")
End Using

Dim output As StringBuilder = New StringBuilder()

Using reader As XmlReader = XmlReader.Create(New StringReader(XML))
 reader.ReadToFollowing("url")
 reader.MoveToFirstAttribute()
 reader.ReadToFollowing("loc")
 output.AppendLine("<hr/>URL: " + reader.ReadElementContentAsString())
End Using

HttpContext.Current.Response.Write(output.ToString())

%>

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 39724559
Have you tried using

While reader.read()

loop?
0
 
LVL 16

Author Comment

by:hankknight
ID: 39724583
This gives an error:
<%@ Page Language="VB" %>
<%@Import namespace="System"%>
<%@Import namespace="System.Xml"%>
<%@Import namespace="System.IO"%>
<%@Import namespace="System.Text"%>
<%

Dim XML As String
Using WC As New System.Net.WebClient()
 XML = WC.DownloadString("http://www.freesitemapgenerator.com/sitemap.xml")
End Using

Dim output As StringBuilder = New StringBuilder()

Using reader As XmlReader = XmlReader.Create(New StringReader(XML))
 While reader.read()
  reader.ReadToFollowing("url")
  reader.MoveToFirstAttribute()
  reader.ReadToFollowing("loc")
  output.AppendLine("<hr/>URL: " + reader.ReadElementContentAsString())
 End While
End Using

HttpContext.Current.Response.Write(output.ToString())

%>

Open in new window

The ReadElementContentAsString method is not supported on node type None. Line 1616, position 1.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 39724960
You need a loop along the lines of:
        While reader.ReadToFollowing("url")
            If reader.ReadToDescendant("loc") Then
                output.AppendLine("<hr/>URL: " + reader.ReadElementContentAsString())
            End If
        End While

Open in new window

0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question