• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 945
  • Last Modified:

How to read the attribute of an xml node using XmlTextReader

Hello,

Using....
ASP.NET
XmlTextReader

I need to read the attribute of an xml node.
Here's a sample xml doc...

<?xml version="1.0" ?>
<Foods>
<FoodGroup Fruit="apple" Veggie="Carrot" />
</Foods>

How do I extract the value of 'Fruit' ?

Thanks,

--- Mike

0
MikeCombe
Asked:
MikeCombe
1 Solution
 
naveenkohliCommented:
Here is some psuedo code.

XmlDocument doc = new XmlDocument();
doc.LoadXml('---- your xml stream ---");

XmlNode oNode = doc.DocumentElement.SelectNode("Foods/FoodGroup");
if (node != null)
{
  String strValue = oNode.Attributes["Fruit"].Value;
}
0
 
MikeCombeAuthor Commented:
Hi Naveenkohi,

Actually I was looking for an XmlTextReader solution.
This works....

dim sr as new StringReader(SomeXmlDoc)
dim xmlReader as  new XmlTextReader(sr)
xmlReader.WhiteSpaceHandling = WhiteSpaceHandling.NONE
if xmlReader.Name.Equals("FoodGroup")       then
     curItem = xmlReader.GetAttribute("Fruit")
End if

THANKS for your help!

--- Mike
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now