This tutorial will show you how to calculate average of data from XML using ASP.NET 2.0 and VB.
This tutorial will show you how to calculate average of data from XML using ASP.NET 2.0 and C#. 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. XPath expressions as a string, or a compiled XPathExpression that return a W3C XPath type of boolean (System.Boolean), number (System.Double), string (System.String), or node set (System.Xml.XPath.XPathNodeIterator), can be passed to the Evaluate method. The Evaluate method takes the expression, evaluates it, and returns a typed result. This method could be used in a mathematical user defined method. For example, the following code calculates the total price of all item elements and div number of book in the current selection.
XPathNavigator class Provides a cursor model for navigating and editing XML data.
XPathDocument class Provides a fast, read-only cache for XML document processing using XSLT.
XPathNodeIterator class Provides an iterator over a selected set of nodes.
| Imports System.Xml Imports System.Xml.XPath Imports System.Data.SqlClient |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
The following code of Button1_Click uses the Evaluate method with the average function.
| Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click docNav = New XPathDocument(Server.MapPath("Books.xml")) End Sub nav = docNav.CreateNavigator() strExpression = "sum(/bookstore/book/price) div count(/bookstore/book/price)" Label1.Text = nav.Evaluate(strExpression).ToString() |
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 front end AverageXmlDataVB.aspx page looks something like this:
| <div> <br /> <table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#5482fc"> <tr> <td height="50" align="center" class="lgHeader1"> How to Average xmldata using ASP.NET 2.0 and VB</td> </tr> </table> <div align="center"> <fieldset style="width: 589px"> <legend>Average Price</legend> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <Columns> <asp:BoundField DataField="title" HeaderText="Title" /> <asp:BoundField DataField="author" HeaderText="Author" /> <asp:BoundField DataField="price" HeaderText="Price" /> </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:GridView> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Average price" /> <asp:Label ID="Label1" runat="server" Text="Show averagedata from xml document:"></asp:Label></fieldset> </div> |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
The flow for the code behind page is as follows.
| Imports System.Data Imports System.Data.SqlClient Imports System.Xml Imports System.Xml.XPath Partial Class _Default Inherits System.Web.UI.Page End Class Dim nav As XPathNavigator Dim docNav As XPathDocument Dim nodeIter As XPathNodeIterator Dim strExpression As String Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim myDs As DataSet = New DataSet() End SubmyDs.ReadXml(Server.MapPath("Books.xml")) Me.GridView1.DataSource = myDs.Tables("book").DefaultView Me.GridView1.DataBind() myDs.Clear() Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click docNav = New XPathDocument(Server.MapPath("Books.xml")) End Subnav = docNav.CreateNavigator() strExpression = "sum(/bookstore/book/price) div count(/bookstore/book/price)" Label1.Text = nav.Evaluate(strExpression).ToString() |
No comments:
Post a Comment