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
MUTHU
Jitendra Faye replied to muthuraman alexander on 27-Oct-11 07:44 AM
Follow this link -

http://www.4guysfromrolla.com/articles/071603-1.aspx

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

XQuery

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

XPATH

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:

http://www.codeguru.com/cpp/data/mfc_database/xml/article.php/c7589
http://www.4guysfromrolla.com/articles/071603-1.aspx
http://msdn.microsoft.com/en-us/library/ms345122(v=sql.90).aspx

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" ?>
 
<movieList>
 
<movie id="1">
 
<title>Bad Boys</title>
</movie>
 
<movie id="2">
<title>Chronicles of Narnia</title>
 
</movie>
<movie id="3">
 
<title>The Love Guru</title>
</movie>
 
</movieList>

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

http://

Regards
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) |
                    i
.Element("summary").Value.Contains(sInput))
                   
select i;
           
List FinalResult = new List();
           
foreach (string sPart in searchstring)
           
{
               sInput
= sPart;
               
FinalResult.AddRange(gh.ToList());
           
}
           
foreach (var item in FinalResult.Distinct())
           
{
               
Console.WriteLine(item);
           
} 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).

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

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("doc.xml"));
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="/">
    <html>
    <table>
      <tr>
      <td>ID</td>
      <td>CONTENT</td>
      </tr>
      <xsl:apply-templates select="root/itemsA"/>
      <xsl:apply-templates select="root/itemsB"/>
    </table>
    </html>
  </xsl:template>
  <xsl:template match="root/itemsA">
    <xsl:apply-templates select="item"></xsl:apply-templates>
  </xsl:template>
  <xsl:template match="root/itemsB">
    <xsl:apply-templates select="item"></xsl:apply-templates>
  </xsl:template>

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

 

        XslCompiledTransform trans = new XslCompiledTransform();
        trans.Load(Server.MapPath("tran.xslt"));
        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

thanks
muthu