Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I read attributes for each element?


XML
<main>
     <first>
          <second Att="24">aaaa</second>
          <second Att="12">bbbb</second>
          <second Att="24">cccc</second>
     </first>
     <first>
          <second Att="16">1111</second>
          <second Att="8">2222</second>
          <second Att="12">3333</second>
     </first>
</main>



C#
XDocument xdoc = XDocument.Load("file.xaml");

            foreach (XElement xelem in xdoc.Descendants("first"))
            {

                string s = xelem.Value;
                var attrib = item.Element("second").Attribute("Att").Value; //Give me only the first attribute
Posted
Updated 23-Feb-15 2:20am
v2

 
Share this answer
 
Comments
Member 11470311 23-Feb-15 5:44am    
I have read all the links. But I still can't figure out the right solution.
Try below code:

XmlDocument objXml = new XmlDocument();
        objXml.Load("testXML.xml");

        XmlNodeList objFirst = objXml.SelectNodes("main/first");

        foreach (XmlNode xNode in objFirst)
        {
            foreach (XmlNode xSecondNode in xNode.SelectNodes("second"))
            {
                Response.Write(xSecondNode.Attributes[0].Value.ToString() + "<br/>");
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900