Thursday 9 June 2011

XML - Finding a XML Element by the attribute ID

Straight as it sounds, its now time to demonstrate how to find an element by the element's attribute ID.

in c#

string filename = "categories.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(filename));

XmlNodeList xnList = xmlDoc.SelectNodes("/CategoryList/Category[@ID='02']");
foreach (XmlNode xn in xnList)
{
Console.WriteLine(xn.InnerText);
}


and in vb.net

Dim filename As String = "categories.xml"
Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.Load(Server.MapPath(filename))

Dim xnList As XmlNodeList = xmlDoc.SelectNodes("/CategoryList/Category[@ID='01']")


For Each xn As XmlNode In xnList
MsgBox(xn.InnerText)
Next

No comments:

Post a Comment