Solved

Using XmlReader ... how to read multiple nodes?

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now