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 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 |
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")) nav = docNav.CreateNavigator() strExpression = "sum(/bookstore/book/price) div count(/bookstore/book/price)" Label1.Text = nav.Evaluate(strExpression).ToString() End Sub |
The front end AverageXmlDataCsharp.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 C#</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> |
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
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() myDs.ReadXml(Server.MapPath("Books.xml"))
Me.GridView1.DataSource = myDs.Tables("book").DefaultView Me.GridView1.DataBind() myDs.Clear() End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click docNav = New XPathDocument(Server.MapPath("Books.xml")) nav = docNav.CreateNavigator() strExpression = "sum(/bookstore/book/price) div count(/bookstore/book/price)" Label1.Text = nav.Evaluate(strExpression).ToString() End Sub End Class
|
Filed under: ASP.NET, VB.NET, XPath, XML information items, XML, XPathExpression, XPathDocument, XML items, XPathNodeIterator, SUM, XPathNavigator, Div