?
Solved

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

Posted on 2013-12-17
3
Medium Priority
?
584 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 1000 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 1000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

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 …
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 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