Tuesday, 29 July 2014

How to use XPathNavigator in asp.net xml

asp.net XPathNavigator – How to use XPathNavigator in asp.net xml

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

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



No comments:

Post a Comment