Solved

Using XmlReader ... how to read multiple nodes?

Posted on 2012-03-10
5
819 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
  • 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…

828 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