how to Select XML by attribute value in c#

Posted by Kumar_jay99 under XML on 11/17/2011 | Points: 10 | Views : 4205 | Status : [Member] | Replies : 1
<Platform-info>
<Parent pid=0>
<folder id='' name/>
<folder id='' name/>
</Parent>
<Parent pid=2>
<folder id='' name/>
<folder id='' name/>
</Parent>
</Platform-info>
in above xml how i can select he parent node which id =2...?

Ans -

XmlDocument xml = new XmlDocument();
xml.LoadXml(str); // suppose that str string contains "<Parent>...</Parent>"

XmlNodeList xnList = xml.SelectNodes("/Platform-info/Parent[@pid='2']");
foreach (XmlNode xn in xnList)
{
Console.WriteLine(xn.InnerText);
}




Responses

Posted by: RohiK on: 11/18/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

yes you can do this

XmlNodeList elemList = doc.GetElementsByTagName(...);
for (int i = 0; i < elemList.Count; i++)
{
string attrVal = elemList[i].Attributes["pid"].Value;
if(pid==2)
{
//put your code here
}
}

Happy Programming!!
Rohi

Kumar_jay99, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response