Simple XPath query with dot net
==========================
XML Path Language (XPath) is the capability to query and locate the tree's content in a xml file.
In dot net XPath evaluation is exposed through the XPathNavigator abstract class. The navigator is an XPath processor that works on top of any XML data source that exposes the IXPathNavigable interface.The most important member of this interface is the CreateNavigator method, which returns an XPathNavigator object.
Today we will try out some xpath usin dot net
Format of users.xml
<?xml version="1.0" encoding="utf-8" ?> <users> <user> <name rollno="1">Bobby</name> <password>pass1</password> <role>Manager</role> </user> <user> <name rollno="2">Ravi</name> <password>pass2</password> <role>Data Entry Operator</role> </user></users>
/// using xpath to retrieve names
private void showXMLName() {
XPathDocument xpd = new XPathDocument("c:\\users.xml");
XPathNavigator xpn = xpd.CreateNavigator();
XPathNodeIterator xpi = xpn.Select("//users/user/name");
while (xpi.MoveNext()) {
MessageBox.Show(xpi.Current.Name + ":" + xpi.Current.Value);
}
}
// using normal xml
private void showXMLName() {
string xmlFile = "c:\\users.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
XmlNodeList nodes = doc.GetElementsByTagName("name");
foreach (XmlNode node in nodes) {
MessageBox.Show(node.ChildNodes[0].Value);
}
}
//you might want to go for attributes and other details alltogether
private void showXMLDetails() {
string xmlFile = "c:\\users.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
// Retrieve the title of every science-fiction movie.
XmlNodeList nodes = doc.SelectNodes("//users/user");
foreach (XmlNode node in nodes)
{
//MessageBox.Show(node.ChildNodes[0].Attributes.Count.ToString());
//MessageBox.Show(node.ChildNodes[0].ChildNodes[0].Value);
//MessageBox.Show(node.ChildNodes[0].LastChild.Value);
//get roll no - check the attribute at name node
MessageBox.Show(node.ChildNodes[0].Attributes["rollno"].Value );
// show name - in case of windows.net
MessageBox.Show(node.ChildNodes[0].FirstChild.Value ); //show password
MessageBox.Show(node.ChildNodes[1].FirstChild.Value); //show roles
MessageBox.Show(node.ChildNodes[2].FirstChild.Value);
}
}
//you may specify filters in xpath query.. starts-with, contains, position etc...
//get all records which starts with a particular letter
XPathNodeIterator xpi = xpn.Select("//users/user[starts-with(role,'S')]/name");
//get all records where user role is Sales
XPathNodeIterator xpi = xpn.Select("//users/user[role='Sales']/name");
//get all records which contains the string les
XPathNodeIterator xpi = xpn.Select("//users/user[contains(role,'les')]/name");
//get the record at first position.
XPathNodeIterator xpi = xpn.Select("//users/user[position()=1]/name");
//get all records where role = sales and name is bob.. you may specify and condition XPathNodeIterator xpi = xpn.Select("//users/user[role='Sales' or name='Bob']/name");
//get all records where substring for the node role is S...this demonstrate the use of substring
XPathNodeIterator xpi = xpn.Select("//users/user[substring(role,1,1)='S']/name");