Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value of an XML Attribute in System.Xml.XmlDocument 1

Status
Not open for further replies.

reffek

Programmer
Apr 7, 2005
24
0
0
US
I have some XML, here's a snippet:
<STATUS _Condition="FAILURE" />

That XML is loaded as a System.Xml.XmlDocument

I just want to pull out the value of _Condition. I've searched MSDN and Google and find related topics, but nothing specific to what I need.

Finding the solution on how to pull "FAILURE" out of <STATUS>FAILURE</STATUS> is easy to find. Finding the solution to grabbing the value of an attribute, as in <STATUS _Condition="FAILURE" />, is a little more involved. Perhaps I'm too frustrated, annoyed, and sleep deprived to see what's right in front of my face, but if anyone could please help that would be great.
 
Here's an (untested!) example - it should give you the general idea:

<FIRST><SECOND attrib="MyValue">My Text</SECOND></FIRST>

to return MyValue:
MyXmlDocument.SelectSingleNode("./FIRST/SECOND/@attrib").InnerText

HTH
 
If you're iterating through the nodes you can also use

Code:
If CurrentNode.Name="SECOND" Then
   strValue=CurrentNode.Attributes("attrib").Value
End If

Where CurrentNode is an XMLNode.

You can also use an index as well as attribute name.
 
Okay, now what if there are TWO lines like:

Code:
<STATUS _Condition="FAILURE" />
<STATUS _Condition="FAILURE" />

How do I differentiate between the first and second?

 
Then you can either use their order as an index (not recommended) or you'll have to add an attribute or node value to make them unique

eg using order...
Code:
for each node in conditionnodes
     strValue(index)=node.Attributes("attrib").Value
     index+=1
next

using extra attribute...

<STATUS index="0" _Condition="FAILURE" />
<STATUS index="1" _Condition="FAILURE" />

Code:
for each node in conditionnodes
     index=cint(node.Attributes("index").Value)
     strValue(index)=node.Attributes("attrib").Value
next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top