Using XPath to narrow results from an XML file

// These two lines read config setting from the app.config file. filename = ConfigurationSettings.AppSettings["inputfile"]; filter = Configu rationSettings.AppSettings["filter1"]; XmlDataDocument myDocument = new XmlDataDocument(); myDocument.Load(filename); XmlNodeList myList = myDocument.SelectNodes("//item[@class='" + filter + "']/*"); for (int i=0; i < myList.Count; i++) { label1.Text += "/r/n" + myList[i].InnerText; }

The associated xml file looks like this…

<?xml version="1.0" encoding="utf-8" ?> <itemlist> <item class="vegetable"> <name>Carrot</name> <colour>Orange</colour> </item> <item class="animal"> <name>Elephant</name> <colour>Grey</colour> </item> <item class="mineral"> <name>Quartz</name> <colour>white</colour> </item> </itemlist>

**edit**

notes:

// will match anywhere in the document.
/ will match the next element
./ will match the previous element.