This tutorial was created with Visual Stuiod.NET 2008. It can be recreated in 2005, but you will need to download and install Microsoft's ASP.NET AJAX Extensions from this link.
AJAX and LINQ are two of the main focuses of Microsoft right now; and no wonder - both have huge potential and power behind them.
In this example, we will show how we can use AJAX coupled with LINQ and XML to create a Web Application that we can use to view stored data instantaneously, as well as add to it in the same way. AJAX provides the ease of use, by making the application run smoothly and efficiently, as if it was a desktop application, while LINQ provides the means to communicate and interact with the XML file and data stored within.
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 first thing we need to do is create our XML file. For this example, we will be using something like this:
<?xml version="1.0" encoding="utf-8"?>
<Persons>
<Person>
<Name>Paxton</Name>
<City>Munich</City>
<Age>29</Age>
</Person>
<Person>
<Name>Mike</Name>
<City>Orlando</City>
<Age>33</Age>
</Person>
</Persons> |
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.
We will create a form to both read and write to the XML file: We will need three textboxes and a button for additions, and then a button and a textbox for reading. We will construct a form similar to this:
<form id="form1" runat="server">
<strong>Add to XML</strong><br />
Name:<br />
<asp:TextBox ID="txtName" runat="server" /><br />
City:<br />
<asp:TextBox ID="txtCity" runat="server" /><br />
Age:<br />
<asp:TextBox ID="txtAge" runat="server" /><br />
<asp:Button ID="butAdd" runat="server" Text="Add" onclick="butAdd_Click" /><br />
<asp:Label ID="lblStatus" runat="server" />
<br /><br />
<strong>Read XML:</strong><br />
<asp:Button ID="butRead" runat="server" Text="Read" onclick="butRead_Click" /><br />
<asp:TextBox ID="txtResults" runat="server" Columns="25" Rows="10"
TextMode="MultiLine" />
</form> |
Notice that our buttons have the OnClick handlers pointing to a method. We will get to those in a minute, but first, let's finish off our ASPX page. The only thing left to do is enable AJAX on this page. We do this by adding a ScriptManager and an UpdatePanel, like so:
<form id="form1" runat="server">
<asp:ScriptManager id="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="updAdd" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="butAdd" EventName="Click" />
</Triggers>
<ContentTemplate>
<strong>Add to XML</strong><br />
Name:<br />
<asp:TextBox ID="txtName" runat="server" /><br />
City:<br />
<asp:TextBox ID="txtCity" runat="server" /><br />
Age:<br />
<asp:TextBox ID="txtAge" runat="server" /><br />
<asp:Button ID="butAdd" runat="server" Text="Add" onclick="butAdd_Click" /><br />
<asp:Label ID="lblStatus" runat="server" />
<br /><br />
<strong>Read XML:</strong><br />
<asp:Button ID="butRead" runat="server" Text="Read" onclick="butRead_Click" /><br />
<asp:TextBox ID="txtResults" runat="server" Columns="25" Rows="10"
TextMode="MultiLine" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
|
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
Before we start coding, we should make sure we have the correct namespaces. We will be making use of LINQ to XML, so we need System.Xml.Linq.
Our directives will look something like this:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; |
Now back to the buttons. We already added our handlers to the buttons, so we're now done with the ASPX page and now we can code our methods. We will create a method for the reading of the XML file, as we are likely to use this more than once (other than just on the button click - you'll see why). Our method will look something like this:
protected void readXML()
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("People.xml"));
var persons = from person in xmlDoc.Descendants("Person")
select new
{
Name = person.Element("Name").Value,
City = person.Element("City").Value,
Age = person.Element("Age").Value,
};
txtResults.Text = "";
foreach (var person in persons)
{
txtResults.Text = txtResults.Text + "Name: " + person.Name + "\n";
txtResults.Text = txtResults.Text + "City: " + person.City + "\n";
txtResults.Text = txtResults.Text + "Age: " + person.Age + "\n\n";
}
if (txtResults.Text == "")
txtResults.Text = "No Results.";
} |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
This method makes use of LINQ to first connect with the XML file and then make a selection of all the data within. Once selected, we loop each 'record' and output to the textbox control.
We call this method from the button click event:
protected void butRead_Click(object sender, EventArgs e)
{
readXML();
lblStatus.Text = "";
} |
Next up is adding to the XML file. This actually requires less code than reading. We will code this directly into the button click event. It will look something like this:
protected void butAdd_Click(object sender, EventArgs e)
{
try
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("People.xml"));
xmlDoc.Element("Persons").Add(new XElement("Person", new XElement("Name", txtName.Text),
new XElement("City", txtCity.Text), new XElement("Age", txtAge.Text)));
xmlDoc.Save(Server.MapPath("People.xml"));
lblStatus.Text = "Data successfully added to XML file.";
readXML();
}
catch
{
lblStatus.Text = "Sorry, unable to process request. Please try again.";
}
} |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
We use a try, catch to reduce the number of errors in processing. Again, we are using LINQ to add data to the XML file - first we load the file, and then we simply add a new element into the parent element, referring back to our XML structure will be useful for understanding.
The entire code-behind will look something like this:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void butRead_Click(object sender, EventArgs e)
{
readXML();
lblStatus.Text = "";
}
protected void butAdd_Click(object sender, EventArgs e)
{
try
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("People.xml"));
xmlDoc.Element("Persons").Add(new XElement("Person", new XElement("Name", txtName.Text),
new XElement("City", txtCity.Text), new XElement("Age", txtAge.Text)));
xmlDoc.Save(Server.MapPath("People.xml"));
lblStatus.Text = "Data successfully added to XML file.";
readXML();
}
catch
{
lblStatus.Text = "Sorry, unable to process request. Please try again.";
}
}
protected void readXML()
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("People.xml"));
var persons = from person in xmlDoc.Descendants("Person")
select new
{
Name = person.Element("Name").Value,
City = person.Element("City").Value,
Age = person.Element("Age").Value,
};
txtResults.Text = "";
foreach (var person in persons)
{
txtResults.Text = txtResults.Text + "Name: " + person.Name + "\n";
txtResults.Text = txtResults.Text + "City: " + person.City + "\n";
txtResults.Text = txtResults.Text + "Age: " + person.Age + "\n\n";
}
if (txtResults.Text == "")
txtResults.Text = "No Results.";
}
} |