Tutorial RSS
 
Navigator: Home - Display - Display XML Data using XMLDataSource and C#

Display XML Data using XMLDataSource and C#

This tutorial will show you how to display XML Data using the XMLDataSource control, ASP.NET 2.0, and C#.NET


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!


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

Looking for more DB Tutorials? Click Here!

The .NET Framework offers a simple tool called XMLDataSource to display XML data.

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();
}
}

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.



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">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
<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.

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 DB Tutorials? Click Here!

Try Server Intellect for Windows Server Hosting. Quality and Quantity!


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!

411asp.net123aspxDotNetFreaksServer Intellect