How to read xml string in vb.net?

  • Thread starter Thread starter Terrence Chan
  • Start date Start date
T

Terrence Chan

I have a string with xml syntax.

<department>
<employee name="ABC" age="31" sex="male"/>
<employee name="CDE" age="40" sex="male"/>
</department>

How do I get the value from the xml string?

Thanks!
 
I have a string with xml syntax.

<department>
<employee name="ABC" age="31" sex="male"/>
<employee name="CDE" age="40" sex="male"/>
</department>

How do I get the value from the xml string?

WHICH value?
 
* Terrence Chan said:
I have a string with xml syntax.

<department>
<employee name="ABC" age="31" sex="male"/>
<employee name="CDE" age="40" sex="male"/>
</department>

How do I get the value from the xml string?

For processing XML data, take a look at the classes in the 'System.Xml'
namespace. For reading files, take a look at the classes in the
'System.IO' namespace, namely the 'StreamReader' class.
 
Terrence,

I made a simple sample from your question.

I hope it helps?

Cor
\\\\
Dim xmlString As String = "<department>" & _
"<employee name=""ABC"" age=""31"" sex=""male""/>" & _
"<employee name=""CDE"" age=""40"" sex=""male""/></department>"
Dim sr As New System.IO.StringReader(xmlString)
Dim doc As New Xml.XmlDocument
doc.Load(sr)
'or just in this case doc.LoadXML(xmlString)
Dim reader As New Xml.XmlNodeReader(doc)
While reader.Read()
Select Case reader.NodeType
Case Xml.XmlNodeType.Element
If reader.Name = "employee" Then
MessageBox.Show(reader.GetAttribute("name"))
End If
End Select
End While
///
 
Hi Cor,

Thank you so much for your code. It works very well.

I know it is something about the class system.xml as Herfried mentioned.
But I always have hard time to read the msdn document and found out how
the code should write. They explain the class and description not well.

May I ask how do you get the ideal of using the code below as there is
so many class and event under the system.xml class?

Thanks,
Terrence
 
Hi Cor,

Thank you so much for your code. It works very well!!!

I know it is something about the class system.xml as Herfried mentioned.
But I always have hard time to read the msdn document and found out how
the code should write. They explain the class and description not well.

May I ask how do you get the ideal of using the code you provided as
there is so many class and event under system.xml?

Thanks,
Terrence
 
Terrence,

I agree with you that it is one of the worsest documentated classes there
are in VBNet.

Just bringing all together will get you it working, however than it is quiet
simple as you probably saw?

Cor
"
 

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