Solved

Using XmlReader ... how to read multiple nodes?

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

Independent Software Vendors: 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

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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