Tuesday, 29 July 2014

ASP.NET XPathNavigator – How to compile XPathExpression

XPathNavigator Compile() – How to compile Expression in asp.net XML

  1. <%@ Page Language=“C#” AutoEventWireup=“true” %>
  2. <%@ Import Namespace=“System.Xml” %>
  3. <%@ Import Namespace=“System.Xml.XPath” %>
  4. <%@ Import Namespace=“System.Drawing” %>

  5. <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
  6. <script runat=“server”>
  7.     void Page_Load(object sender, System.EventArgs e)
  8.     {
  9.         if (!Page.IsPostBack)
  10.         {
  11.             BulletedList1.ForeColor = Color.DeepPink;
  12.             BulletedList1.Font.Name = “Comic Sans MS”;
  13.             BulletedList1.Font.Size = FontUnit.Large;
  14.             BulletedList1.BulletStyle = BulletStyle.LowerRoman;
  15.         }
  16.     }
  17.     void Button1_Click(object sender, System.EventArgs e)
  18.     {
  19.         BulletedList1.Items.Clear();
  20.         string xmlPath = Request.PhysicalApplicationPath + @”App_Data\ITBookList.xml”;
  21.         XPathDocument xPathDoc = new XPathDocument(xmlPath);
  22.         XPathNavigator xPathNavigator = xPathDoc.CreateNavigator();
  23.         XPathExpression xPathExpr = xPathNavigator.Compile(TextBox1.Text);
  24.         XPathNodeIterator nodes = xPathNavigator.Select(xPathExpr);
  25.         while (nodes.MoveNext())
  26.         {
  27.             BulletedList1.Items.Add(nodes.Current.Value);
  28.         }
  29.     }
  30. </script>
  31. <html xmlns=“http://www.w3.org/1999/xhtml” >
  32. <head id=“Head1″ runat=“server”>
  33.     <title>XPathNavigator Compile() - How to compile XPathExpression in asp.net xml</title>
  34. </head>
  35. <body>
  36.     <form id=“form1″ runat=“server”>
  37.     <div>
  38.         <h2 style=“color:IndianRed; font-style:italic;”>XPathNavigator Example: How To Use Compile() Method</h2>
  39.         <hr width=“575″ align=“left” color=“Pink” />
  40.         <asp:BulletedList
  41.             ID=“BulletedList1″
  42.             runat=“server”
  43.             >
  44.         </asp:BulletedList>
  45.         <br /><br />
  46.         <asp:Label
  47.             ID=“Label2″
  48.             runat=“server”
  49.             Font-Bold=“true”
  50.             Text=“XPath Expression”
  51.             ForeColor=“IndianRed”
  52.             >
  53.         </asp:Label>
  54.         <asp:TextBox
  55.             ID=“TextBox1″
  56.             runat=“server”
  57.             Text=“//book/name”
  58.             ForeColor=“Snow”
  59.             Width=“325″
  60.             BackColor=“IndianRed”
  61.             Height=“35″
  62.             Font-Bold=“true”
  63.             >
  64.         </asp:TextBox>
  65.         <br /><br />
  66.         <asp:Button
  67.             ID=“Button1″
  68.             runat=“server”
  69.             OnClick=“Button1_Click”
  70.             Text=“Evaluate XPath Expression”
  71.             Height=“40″
  72.             Font-Bold=“true”
  73.             ForeColor=“IndianRed”
  74.             />
  75.     </div>
  76.     </form>
  77. </body>
  78. </html>



No comments:

Post a Comment