This tutorial will show you how to navigate XML information items in ASP.NET 2.0 using VB.NET.
First, import the namespace of System.XML and System.XML.XPath
The System.Xml.XPath namespace contains the classes that define a cursor model for navigating and editing XML information items as instances of the XPath 2.0 Data Model. In this sample, we will use the classes of XPathDocument, XPathNavigator, XPathExpression and XPathNodeIterator under this namespace .
| Imports System.Xml Imports System.Xml.XPath Imports System.Data |
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
The Button1_Click event is to perform the navgating XML infomration items fuction. The results will be displayed in the textbox. There are 4 classes we applied.
XPathDocument: Provides a fast, read-only, in-memory representation of an XML document using the XPath data model.
XPathNavigator: Provides a cursor model for navigating XML data.
XPathExpression: Provides a typed class that represents a compiled XPath expression.
XPathNodeIterator: Provides an iterator over a selected set of nodes.
| Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim doc = New XPathDocument(Server.MapPath("Demo.xml")) End Sub Dim nav = doc.CreateNavigator() Dim xpathExpress = nav.Compile("//bk:Book[position()" + Me.ddownlist.SelectedValue.Trim() + Me.txtNum.Text.Trim() + "]") 'use AddNamespace Dim xmlManager = New XmlNamespaceManager(nav.NameTable) xmlManager.AddNamespace("bk", "http://myserver/myschemas/Books") xpathExpress.SetContext(xmlManager) Dim xIterator = nav.Select(xpathExpress) Me.TextBox1.Text = "" Me.TextBox1.ForeColor = System.Drawing.Color.Empty Do While xIterator.MoveNext() Me.TextBox1.Text = Me.TextBox1.Text + ControlChars.CrLf + xIterator.Current.Value LoopIf (xIterator.Count.Equals(0)) Then Me.TextBox1.Text = "There is no xml data in the condition." End IfMe.TextBox1.ForeColor = System.Drawing.Color.Red |
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
The contents of Demo.xml:| <?xml version='1.0' encoding='utf-8'?> <bk:Books xmlns:bk='http://myserver/myschemas/Books'> <bk:Book> </bk:Books> <bk:Title>Just XML</bk:Title> </bk:Book> <bk:Book> <bk:Title>Professional XML</bk:Title> </bk:Book> <bk:Book> <bk:Title>XML Step by Step</bk:Title> </bk:Book> <bk:Book> <bk:Title>XML By Example</bk:Title> </bk:Book> |
The front end Default.aspx page looks something like this:
| <body> <form id="form1" runat="server"> </body> <div> <fieldset><legend>Select xml data with XPath</legend> </div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" </fieldset>Font-Size="Smaller" ForeColor="#333333" GridLines="None" RowHeaderColumn="Title" Width="205px"> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> </asp:GridView> <br /><Columns> <asp:BoundField DataField="Title" HeaderText="Title" /> </Columns> <RowStyle BackColor="#EFF3FB" /> <EditRowStyle BackColor="#2461BF" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <asp:Label ID="Label1" runat="server" Text="Select Item Position:"></asp:Label> <asp:DropDownList <asp:TextBox ID="txtNum" runat="server" Width="57px">2</asp:TextBox>ID="ddownlist" runat="server"> <asp:ListItem><</asp:ListItem> </asp:DropDownList><asp:ListItem><=</asp:ListItem> <asp:ListItem>></asp:ListItem> <asp:ListItem>>=</asp:ListItem> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtNum" ErrorMessage="Please input integer number."></asp:RequiredFieldValidator><br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" /><br /> <asp:TextBox ID="TextBox1" runat="server" Height="88px" TextMode="MultiLine" Width="237px"></asp:TextBox> </form> |
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
The flow for the code behind page as follows.| Imports System.Xml Imports System.Xml.XPath Imports System.Data Partial Class _Default Inherits System.Web.UI.Page End Class Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim doc = New XPathDocument(Server.MapPath("Demo.xml")) End SubDim nav = doc.CreateNavigator() Dim xpathExpress = nav.Compile("//bk:Book[position()" + Me.ddownlist.SelectedValue.Trim() + Me.txtNum.Text.Trim() + "]") 'use AddNamespace Dim xmlManager = New XmlNamespaceManager(nav.NameTable) xmlManager.AddNamespace("bk", "http://myserver/myschemas/Books") xpathExpress.SetContext(xmlManager) Dim xIterator = nav.Select(xpathExpress) Me.TextBox1.Text = "" Me.TextBox1.ForeColor = System.Drawing.Color.Empty Do While xIterator.MoveNext() Me.TextBox1.Text = Me.TextBox1.Text + ControlChars.CrLf + xIterator.Current.Value LoopIf (xIterator.Count.Equals(0)) Then Me.TextBox1.Text = "There is no xml data in the condition." End IfMe.TextBox1.ForeColor = System.Drawing.Color.Red Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then End SubDim myDs = New DataSet() End IfmyDs.ReadXml(Server.MapPath("Demo.xml")) Me.GridView1.DataSource = myDs Me.GridView1.DataBind() |
No comments:
Post a Comment