How to read the attribute of an xml node using XmlTextReader

Posted on 2006-04-19
Last Modified: 2012-08-14


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

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

How do I extract the value of 'Fruit' ?


--- Mike

Question by:MikeCombe
    LVL 23

    Accepted Solution

    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;

    Author Comment

    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

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now