Parsing XML strings

  • Thread starter Thread starter almurph
  • Start date Start date
A

almurph

Hi,

I have XML data in a string format and want to extract the content
and parse it. Can anyone suggest any methods/techniques/ways to do this
please?

Any comments/suggestions/code-snippets much appreciated.

Al
The XML newbie
 
I have XML data in a string format and want to extract the content
and parse it. Can anyone suggest any methods/techniques/ways to do this
please?

You could load the string into an 'XmlDocument' object and use its methods
to locate the content.
 
As Herfried suggests, the DOM can be used and I would guess recommended
for a very simple XML file. But I have recently been impressed with the
power of .NET XMLSerialization. Even for a simple XML file, it doesn't
take very long to set up the class(s) and properties, and to read it in
or write it out only takes two method calls.
 
Just did that. Here's what I did to retrieve the account_number and
reply_code fields from the xml string. Hope it helps.


Dim doc As New XmlDocument
doc.LoadXml(xmlString)

Dim actNbr As XmlNodeList = doc.GetElementsByTagName("account_number")
Dim replyCode As XmlNodeList = doc.GetElementsByTagName("reply_code")

MessageBox.Show("Account: " & actNbr(0).InnerText)
MessageBox.Show("Reply code: " & replyCode(0).InnerText)
 
Back
Top