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#.
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.
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;
|
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
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();
}
|
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers
and add-ons were setup swiftly, in less than 24 hours. We were able to
confirm our order over the phone. They respond to our inquiries within
an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
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>
|
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 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();
}
}
|
|