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.

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.

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:

The flow for the code behind page is as follows.

Download Source Files