Solved

Using XmlReader ... how to read multiple nodes?

Posted on 2012-03-10
5
842 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

726 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