DotNet Tutorials

Server Intellect

 Average Xml Data using ASP.NET and C#

This tutorial will show you how to calculate average of data from XML using ASP.NET 2.0 and C#. 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.

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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.
using System.Xml;
using System.Xml.XPath;
using System.Data.SqlClient;

The following code of  Button1_Click uses the Evaluate method with the average function.

protected void Button1_Click(object sender, EventArgs e)
{
docNav = new XPathDocument(Server.MapPath("Books.xml"));
nav = docNav.CreateNavigator();
strExpression = "sum(/bookstore/book/price) div count(/bookstore/book/price)";
expression.
Label1.Text = nav.Evaluate(strExpression).ToString();
}

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>
&nbsp;
<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.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using System.Xml.XPath;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
XPathNavigator nav;
XPathDocument docNav;
XPathNodeIterator nodeIter;
string strExpression;

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataSet myDs = new DataSet();
myDs.ReadXml(Server.MapPath("Books.xml"));

this.GridView1.DataSource = myDs.Tables["book"].DefaultView;
this.GridView1.DataBind();
myDs.Clear();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
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();
}
}



Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect