?
Solved

Using XmlReader ... how to read multiple nodes?

Posted on 2012-03-10
5
Medium Priority
?
850 Views
Last Modified: 2012-03-12
I can read a SINGLE node, but unable to read MULTIPLE nodes.
What am I doing wrong ?


This works........................................
<WebMethod()> _
Public Shared Function PingResponse(myXML As String) As String
Dim valPetType As String = ""
Dim valPetColor As String = ""
Dim xmlOutput As String = ""
Dim delHeader As String = (myXML .Replace("<?xml version=""1.0"" encoding=""UTF-8"" ?>", String.Empty)
using reader As XmlReader = XmlReader.Create(New StringReader(delHeader))
    reader.ReadToFollowing("PetType")
    valPetType = reader.ReadElementContentAsString().Trim()
End Using
xmlOutput = "<?xml version=""1.0"" encoding=""UTF-8"" ?><xmlResponse><PetType>" + valPetType + "</PetType><PetColor>" + valPetColor + "</PetColor></xmlResponse>"
Return xmlOutput
End Function



This does NOT work.....................................................
<WebMethod()> _
Public Shared Function PingResponse(myXML As String) As String
Dim valPetType As String = ""
Dim valPetColor As String = ""
Dim xmlOutput As String = ""
Dim delHeader As String = (myXML .Replace("<?xml version=""1.0"" encoding=""UTF-8"" ?>", String.Empty)
using reader As XmlReader = XmlReader.Create(New StringReader(delHeader))

    reader.ReadToFollowing("PetType")
    valPetType = reader.ReadElementContentAsString().Trim()

    reader.ReadToFollowing("PetColor")
    valPetColor = reader.ReadElementContentAsString().Trim
End Using
xmlOutput = "<?xml version=""1.0"" encoding=""UTF-8"" ?><xmlResponse><PetType>" + valPetType + "</PetType><PetColor>" + valPetColor + "</PetColor></xmlResponse>"
Return xmlOutput
End Function
0
Comment
Question by:MikeCombe
[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
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37706212
In myXML, does "PetColor" come before "PetType"?
0
 

Author Comment

by:MikeCombe
ID: 37706690
The xml order is......

PetType
PetColor
0
 
LVL 16

Accepted Solution

by:
Rose Babu earned 2000 total points
ID: 37709426
Hi,

replace your code with this part and test the values returned
Using reader As XmlReader = XmlReader.Create(New StringReader(delHeader))
	'reader.ReadToFollowing("PetType")
	'valPetType = reader.ReadElementContentAsString().Trim()

	'reader.ReadToFollowing("PetColor")
	'valPetColor = reader.ReadElementContentAsString().Trim

	Dim xmlDoc As New XmlDocument
	xmlDoc.Load(reader)

	Dim root As XmlElement = xmlDoc.DocumentElement

	valPetType = root.GetElementsByTagName("PetType").Item(0).InnerText
	valPetColor = root.GetElementsByTagName("PetColor").Item(0).InnerText

End Using

Open in new window

Hope this will return the xml string with PetType and PetColor as u need.
0
 

Author Closing Comment

by:MikeCombe
ID: 37709611
Great solution, thanks !
I also did it this way.....

            Using reader As XmlReader = XmlReader.Create(New StringReader(delHeader))
                reader.MoveToContent()
                reader.Read()

                reader.ReadStartElement("PetType")
                valPetType = reader.ReadString
                reader.ReadEndElement()

                reader.ReadStartElement("PetColor")
                valPetColor = reader.ReadString
                reader.ReadEndElement()

              End Using
0
 
LVL 16

Expert Comment

by:Rose Babu
ID: 37710060
Nice. Thanks for sharing your own solution :-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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