Solved

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

Posted on 2013-12-17
3
580 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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