XML Attribute Value

G

Guest

Hi All

I have a xml file in the format below:
<root>
<selection-attr select-style="dropdown" attr="a123">
<value code="A">Responce A</value>
<value code="B">Responce B</value>
<value code="C">Responce C</value>
<value code="D">Responce D</value>
<caption>Question Text</caption>
</selection-attr>
</root>

How do I go about extracting the attr vale from :
<selection-attr select-style="dropdown" attr="a123">

I am trying to read the value a123.

Any assistance would be appreciated.

Thanks
 
G

Gerry O'Brien [MVP]

Have you looked into XPath?

Use an XML document to load the XML file and then using XPath, you can
iterate through the xml file and extract the attribute value.

I just reformatted my machine and VS is not installed right now so I can't
give you a working example right off but I know there are examples in MSDN.

I will try this out in the morning and post some code for you if your
question has not already been answered by that time.
 
C

Cor Ligthert

David,

Does this little sample I once made help you?

\\\\
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
///

I hope this helps a little bit?

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

Top