Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i have a xml and i want to read the attribute values, how do i do that?
xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<ContentMap tag="sdsdsadsadsadasdsadas">
  <ContentNode type="BusinessProcess" name="MainProcess" url="mainbprocess.svg" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f422">
    <ContentNode type="BusinessProcess" name="Sub Bus Process1" url="subbprocess1.svg" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f423">
      <ContentNode type="ApplicationProcess" name="Sub App Process1" url="subappprocess1.svg" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f424">
        <ContentNode type="ReusableApplicationComponent" name="Sub App Component1" url="subappcomponent1.html" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f427">
        </ContentNode>
        <ContentNode type="CustomApplicationComponent" name="Sub App Component2" url="subappcomponent2.html" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f428">
        </ContentNode>
      </ContentNode>
    </ContentNode>
    <ContentNode type="ApplicationProcess" name="Sub App Process2" url="subappprocess2.svg" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f425">
      <ContentNode type="ReusableApplicationComponent" name="Sub App Component3" url="subappcomponent3.html" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f429">
      </ContentNode>
    </ContentNode>
  </ContentNode>
</ContentMap>
Posted

You need to use XMLReader to read the XML. check following code.
C#
System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(\\XML_File_path);
while(xmlReader.Read())
{
   if(XmlNodeType.Attribute)
   {
     //get AttributeName and Value
     Response.Write(xmlReader.Name.ToString()+ ": " + xmlReader.Value.ToString()));                    
    }
}
 
Share this answer
 
v2
Comments
RaviRanjanKr 29-Nov-11 4:49am    
My 5+
gokulnath1 29-Nov-11 5:00am    
thank u :)
RaisKazi 29-Nov-11 5:01am    
My 5.
koolprasad2003 29-Nov-11 5:15am    
Thanks to all
Try
C#
XmlNodeList elemList = doc.GetElementsByTagName(...);
   for (int i = 0; i < elemList.Count; i++)
   {
       string attrVal = elemList[i].Attributes["AttributeName"].Value;
   }

or you can also try
XmlTextReader: read all attributes[^]
Read XML node attributes data in C#[^]
 
Share this answer
 
v2
Comments
gokulnath1 29-Nov-11 4:56am    
thanks a lot....:) it worked... :)
RaviRanjanKr 29-Nov-11 5:11am    
I glad! I helped you.:)
RaisKazi 29-Nov-11 5:02am    
My 5.
RaviRanjanKr 29-Nov-11 5:11am    
Thanks :)
a much simpler way:

C#
foreach (XmlNode n1 in list)
            {
                string str = n1.Attributes["name"].Value;
            }


this string str will have the value of the attribute name of each node, likewise just give the name of the attribute u want and we can access the value.so simple :)
 
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