Simple XML question

  • Thread starter Thread starter Wardeaux
  • Start date Start date
W

Wardeaux

All,
this XML is killing me... I'm new to XML, all I want to do is parse an XML
string so that I can read the values...

Mystr = "<A1><B1><C1>myC1</C1><C2>myC2</C2></B1><B2>myB2</B2><A1>"

All I need is a simple routine to parse this and display Name/Value pairs in
a MSGBOX like:
C1:myC1
C2:myC2
B2:myB2

All assistance is MOST appreciated!!!
wardeaux
 
Wardeaux said:
this XML is killing me... I'm new to XML, all I want to do is parse an XML
string so that I can read the values...

Mystr = "<A1><B1><C1>myC1</C1><C2>myC2</C2></B1><B2>myB2</B2><A1>"

Another way is to create an XmlDocument and use LoadXml to parse the string.

Dim doc As XmlDocument = New XmlDocument( )
doc.LoadXml( Mystr)
' . . . do things to read from doc.

Most direct starting point begins from the DocumentElement of doc, and then
you navigate the objects it gives you called XmlNodes through properties like
ChildNodes, et al.

Each XmlNode has a LocalName property (i.e., A1, B1, C2) if it's NodeType
= Element, and it has a Value property (i.e., "myC1", "myC2") if it's NodeType
= Text (at least, these are the properties most likely to interest you aside from
navigating from one XmlNode to the next). The Text nodes will be children of
the Element nodes that contain them.

Other more powerful approaches include SelectSingleNode( ) and SelectNodes( ),
but these require a good background in XPath 1.0; and there's also the venerable
GetElementsByTagName( ). For an example of the latter,

Dim node As XmlNode
Dim nodes As XmlNodeList
nodes = doc.GetElementsByTagName( "C1")
For Each node In nodes
'
' Do something with this node (it's NodeType will be Element) named C1.
'
Next node


Derek Harmon
 
Sounds like homework at CSU.
I wish... ;)
one way is to read one char at a time and parse
Sorry... parsing one char at a time is slow, inefficient and impractical...
The structure I will be parsing has many records and many levels within the
record... I know MS has spent a lot of $$ on XML and the associated classes,
I'm just needing a place to start digging in on it all...
thanks for the reply, though...
wardeaux
 
Derek,
thanks for the reply... This is a great starting point!!
In real life I'm going to be receiving a stream with multiple <a1>
records... will the SelectSingleNode work better for extracting each record
in sequence or do I need another approach?
thanks again!!
wardeaux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top