Solved

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

Posted on 2013-12-17
3
575 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
3 Comments
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
Comment Utility
Have you tried using

While reader.read()

loop?
0
 
LVL 16

Author Comment

by:hankknight
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Round a string to two digits 12 22
Form design in vb.net 7 16
Re-position the objects 7 45
Jquery autocomplete 10 17
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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now