How to Use an XPath on a DataSet

This small snippet shows how to use a XPath on a DataSet.

Here are the used classes:

DataRowDataSetXmlDataDocumentXmlNodeListXmlNode

Here’s the XML file:

   One     Three     Four     Two  

Finally, here’s the code:

DataSet ds = new DataSet();ds.ReadXml(Server.MapPath("file.xml"));XmlDataDocument xmlDoc = new XmlDataDocument(ds);       XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/CategoryList/Category[@ID=01]");   DataRow myRow;foreach (XmlNode myNode in nodeList){   myRow = xmlDoc.GetRowFromElement((XmlElement)myNode);   if (myRow != null)      Response.Write(myRow[0]);}

And the output?

One
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: