tags:

views:

277

answers:

3

Suppose I get a XmlNode and I want to attribute value of attirbute "Name". How can I do that??

       XmlTextReader reader = new XmlTextReader(path);

        XmlDocument doc = new XmlDocument();
        XmlNode node = doc.ReadNode(reader);

        foreach (XmlNode chldNode in node.ChildNodes)
        {
                **//Read the attribute Name**
            if (chldNode.Name == Employee)
            {                    
                if (chldNode.HasChildNodes)
                {
                    foreach (XmlNode item in node.ChildNodes)
                    { 

                    }
                }
            }
        }

XMl Doc:

<Root>
    <Employee Name ="TestName">
    <Childs/>
</Root>
+1  A: 

Try this:

string employeeName = chldNode.Attributes["Name"].Value;
Konamiman
Be careful with this approach. I think if the attribute is not present, then accessing the Value member will cause a Null Reference Exception.
Chris Dunaway
+1  A: 

you can loop through all attributes like you do with nodes

foreach (XmlNode item in node.ChildNodes)
{ 
    // node stuff...

    foreach (XmlAttribute att in item.Attributes)
    {
        // attribute stuff
    }
}
balexandre
A: 

Use

item.Attributes["Name"].Value;

gets the value

rahul