Xml/Xslt - using xpath and xquery in asp.net - Asked By muthuraman alexander on 27-Oct-11 07:39 AM

hi all,
im new to use xpath and xquery in asp.net
how can i implement xpath and xquery in my asp.net/c# projects
when i goen through simple html examples in w3schools site it was not working ..
i dont know whether the mistake with me or not...

please guide me ..need some sample to show how to execute the xpath and xquery in the asp.net

thanks in  advance
Jitendra Faye replied to muthuraman alexander on 27-Oct-11 07:44 AM
Follow this link -


Here you will get tutorial.
dipa ahuja replied to muthuraman alexander on 27-Oct-11 07:47 AM


XQuery is a new XML Query Language, which is a work in progress by the XML Query Working Group of the W3C


The W3C (the World Wide Web Consortium, http://www.w3.org) published the XML 1.0 specification on February 10th, 1998. The XML 1.1 specification was published six years later, on February 4th, 2004. In these six years, XML has taken the industry by storm. XML has become the standard for how to describe and exchange data.

You can find sample here:


Riley K replied to muthuraman alexander on 27-Oct-11 08:04 AM

using xPath it is easy to read XMl nodes using expressions

suppose you have got below XML

<?xml version="1.0" encoding="utf-8" ?>
<movie id="1">
<title>Bad Boys</title>
<movie id="2">
<title>Chronicles of Narnia</title>
<movie id="3">
<title>The Love Guru</title>

Use the following C# code to read the XML nodes using different types of XPath expressions:

// Select all child nodes of root node of XML document
Response.Write(xmlDoc.SelectSingleNode("movieList").InnerText + "<br />");
// Select all child nodes of root node of XML document
// using single "/" expression
Response.Write(xmlDoc.SelectSingleNode("/movieList").InnerText + "<br />");
// Select child nodes of movie node under root node movieList
Response.Write(xmlDoc.SelectSingleNode("movieList/movie").InnerText + "<br />");
// Select node value of title node under movie
Response.Write(xmlDoc.SelectSingleNode("movieList/movie/title").InnerText + "<br />");
// Select title child node where ever it exits under movieList root node
Response.Write(xmlDoc.SelectSingleNode("movieList//title").InnerText + "<br />");
// Select title child node where ever it exits in the XML document
Response.Write(xmlDoc.SelectSingleNode("//title").InnerText + "<br />");

you can refer this link


Suchit shah replied to muthuraman alexander on 27-Oct-11 08:21 AM
string sRawInput = "book  test";
string[] searchstring = sRawInput.Split(' ');
var  m = XDocument.Load("xmlfile1.xml").Descendants("book");
string sInput = string.Empty;
IEnumerable gh = from i in m
where (i.Element("title").Value.Contains(sInput) |
select i;
List FinalResult = new List();
foreach (string sPart in searchstring)
= sPart;
foreach (var item in FinalResult.Distinct())
} Try this...
Suchit shah replied to muthuraman alexander on 27-Oct-11 08:22 AM

XPath you use to get a XmlNodeList or XmlNode from XmlNode/XmlDocument: XmlNodeList = xmlDocument.DocumentElement.SelectNodes("descendant-or-self::someNode[@someAtribute='something']");
XSLT you use in XslCompiledTransform to convert a XML in another like xslCompiledTransform.Load(xsltString); xslCompiledTransform.Transform(xmlReader, xmlWriter).

    <item id="1">this is result</item>
    <item id="2">abc</item>
    <item id="1">fff</item>

XmlDocument doc = new XmlDocument();
XmlNode target = doc.SelectSingleNode("/root/itemsA/item[@id='1']");
string result = target.InnerText;

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
      <xsl:apply-templates select="root/itemsA"/>
      <xsl:apply-templates select="root/itemsB"/>
  <xsl:template match="root/itemsA">
    <xsl:apply-templates select="item"></xsl:apply-templates>
  <xsl:template match="root/itemsB">
    <xsl:apply-templates select="item"></xsl:apply-templates>

  <xsl:template match="item">
    <xsl:for-each select=".">
      <xsl:value-of select="@id"/>
      <xsl:value-of select="."/>


        XslCompiledTransform trans = new XslCompiledTransform();
        trans.Transform(Server.MapPath("doc.xml"), "c://out.html");

muthuraman alexander replied to Suchit shah on 28-Oct-11 06:11 AM
thank u for all to ur esteemed replies....

it worked me in great