
Navigator : Home > Tutorials > Database Tutorials > ...
Displaying XML Data using ASP.NET 2.0 and C# .NET
This tutorial will show you how to display XML Data using the XMLDataSource control, ASP.NET 2.0, and C#.NET
The .NET Framework offers a simple tool called XMLDataSource to display XML data.
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
For this example we will need not need to import any special namespaces and we will have a XMLDataSource control called xmlDS, a Repeater called rptXMLExample, a text box control called txtXML, and a button called btnSubmit. We'll put our code in a subroutine called XMLBind() which will be called by the btnSubmit_Click() and Page_Load() events.
When the btnSubmit_Click() event fires it calls our subroutine which sets our XMLDataSource's Data property to the text in our text box. It then calls its DataBind() method to bind the data to the control fully.
Note: In order to submit XML through a web form the validateRequest="false" directive needs to be added to the top of your page. This is a security feature that is implicitly set to "true" but has been set to "false" for demonstration purposes. It is not recommended to disable this feature in a production environment.
After we have setup our XMLDataSource we need to assign it as our Repeater control's DataSourceID and execute the DataBind() method . This allows the Repeater control to read whatever data is bound to the XMLDataSource control. We are now ready to display our data.
protected void XMLBind() {
try {
//set the XMLDataSource's control to the XML within the Text Box xmlDS.Data = txtXML.Text; //bind the data xmlDS.DataBind(); //set the Repeater Control's Data Source to XMLDataSource rptXMLExample.DataSourceID = "xmlDS"; //bind the data rptXMLExample.DataBind(); } catch (Exception ex) {
lblStatus.Text += ex.ToString(); } } |
The front end .aspx page looks something like this. Note the XPath() subroutine, this accepts regular XPath syntax to return arbitrary XML nodes or elements:
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc"> <tr> <td align="right" bgcolor="#eeeeee" class="header1">Repeater Control:</td> <td bgcolor="#FFFFFF"> <asp:repeater ID="rptXMLExample" runat="server"> <itemtemplate> <strong> <%# XPath("@name") %> </strong><br /> <%# XPath("desc") %> <br /> </itemtemplate> </asp:repeater> </td> </tr> <tr> <td width="100" align="right" bgcolor="#eeeeee" class="header1"> XML:</td> <td bgcolor="#FFFFFF"> <asp:textbox ID="txtXML" runat="server" Rows="10" TextMode="MultiLine" Width="465px"><?xml version="1.0" encoding="utf-8" ?>
<AccessModifiers>
<modifier name="public"> <desc>The member or type is fully accessible.</desc> </modifier>
<modifier name="internal"> <desc>The member or type is accessible from within its assembly</desc> </modifier>
<modifier name="private"> <desc>The member or type is only accessible from within the enclosing type</desc> </modifier>
<modifier name="protected"> <desc>The member or type is only accessible from within the enclosing class or a class that inherits from the enclosing class</desc> </modifier>
<modifier name="protected internal"> <desc>The member or type is only accessible from within the enclosing class or a class that inherits from the enclosing class or from within its assembly</desc> </modifier>
</AccessModifiers></asp:textbox><br /> <asp:button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /> <asp:XmlDataSource ID="xmlDS" runat="server" XPath="AccessModifiers/modifier" EnableCaching="False"></asp:XmlDataSource> <asp:label ID="lblStatus" runat="server"></asp:label></td> </tr> </table> |
The flow for the code behind page is as follows.
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
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;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
XMLBind(); } protected void btnSubmit_Click(object sender, EventArgs e) {
XMLBind(); } protected void XMLBind() {
try {
//set the XMLDataSource's control to the XML within the Text Box xmlDS.Data = txtXML.Text; //bind the data xmlDS.DataBind(); //set the Repeater Control's Data Source to XMLDataSource rptXMLExample.DataSourceID = "xmlDS"; //bind the data rptXMLExample.DataBind(); } catch (Exception ex) {
lblStatus.Text += ex.ToString(); } } } |
Looking for the VB .NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!