Wednesday, August 22, 2007

Simple XPath query with dot net

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");

28 comments:

Anonymous said...

generic xanax can you buy xanax online - buy xanax online overnight

Anonymous said...

tramadol 50 mg can i buy tramadol over the counter in the usa - tramadol overnight delivery no rx

Anonymous said...

buy tramadol online order tramadol online sweden - buy tramadol online safely

Anonymous said...

purchase tramadol cope tramadol addiction - tramadol $89

Anonymous said...

xanax online xanax effects anxiety - can take 2mg xanax once

Anonymous said...

carisoprodol 350 mg what is carisoprodol 350 mg tablet for - carisoprodol nursing implications

Anonymous said...

generic xanax does military drug test xanax - xanax pills and kilograms

Anonymous said...

buy tramadol online generic tramadol pill identification - tramadol hydrochloride for dogs side effects

Anonymous said...

tramadol without prescription tramadol withdrawal flu - recovering tramadol addiction

Anonymous said...

buy tramadol online tramadol apap 37.5mg high - tramadol klonopin

Anonymous said...

buy xanax .25 mg xanax for anxiety - xanax bars feeling

Anonymous said...

cialis online how to buy cialis - cialis 50mg pills

Anonymous said...

buy cialis online generic cialis vs brand cialis - cialis daily dose side effects

Anonymous said...

buy tadalafil online cialis online in the usa - cialis online real

Anonymous said...

cialis online superdrug cialis price - where to buy cheap cialis

Anonymous said...

cialis 10mg buy cialis online new zealand - safe place order cialis online

Anonymous said...

buy tramadol tramadol hcl usp - zodol-tramadol 50 mg

Anonymous said...

buy tramadol online buy tramadol forum - tramadol mg get high

Anonymous said...

http://landvoicelearning.com/#97734 buy tramadol online australia - buy tramadol online pay cod

Anonymous said...

learn how to buy tramdadol tramadol withdrawal long does take - can you get tramadol online legally

Anonymous said...

buy tramadol tramadol for dogs in usa - buy cheap tramadol online

Anonymous said...

learn how to buy tramdadol tramadol hcl is it a narcotic - tramadol buy cod

Anonymous said...

buy ativan online ativan oral dose - ativan overdose signs symptoms

Anonymous said...

http://staam.org/#56821 tramadol 50 mg compared to percocet - what is tramadol the generic of

Anonymous said...

http://staam.org/#56821 tramadol 50mg 627 - legal buy tramadol online usa

Anonymous said...

buy tramadol online tramadol cost - tramadol overdose 1500 mg

Anonymous said...

buy tramadol online tramadol to get high - tramadol for dogs cost

Anonymous said...

xanax online canadian pharmacy no prescription xanax - xanax overdose fatal