XML data can be challenging to display from a database. In this tutorial, you will learn how to display XML data using the XMLDataSource control in Visual Basic.


If you have not already done so, open Visual Studio and attach the database provided to you in the source files, located at the end of the tutorial.

The Concept

The .NET Framework offers a simple tool called an XMLDataSource control to display XML data. For this example we will not need to import any special namespaces and we will have a XMLDataSource control calledxmlDS, 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() andPage_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.

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.

Step one

The code box below will have our code behind, which is written completely in visual basic. It is a simple try-catch block within a protected XMLBind method. If you intend to do the project from scratch, go ahead and copy and paste it onto your default.aspx.vb file.

In the code above, we set the XMLDataSource’s control the the XML within the Text Box, bind the data and set the repeater control’s data source to XMLDataSource. rptXMLExample.DataBind() method binds the data and we close the try part of the block. The catch portion handles the exception and prints out exception string.

Note: the XPath() subroutine, this accepts regular XPath syntax to return arbitrary XML nodes or elements.

Step two

Once we have what we need in our code behind, we will need to setup our default.aspx page. We will need a table with a repeater control, an ItemTemplate in order to place our data binding tags, our XMLDataSource as well as a few other controls to make this happen. Copy and paste the following code into your default.aspx page, or modify it as needed for your own project.

Save and run your code. You will notice that you will come across an error. This is because on Page_Load does not contain the necessary code for the XMLBind() method call and the button OnClick event isn’t even there. Double click your button so that Visual Studio adds it for you in code behind and modify your code behind file to include the code underneath the try-catch block as presented below:

The flow goes as follows: on page load, the XMLBind() method is called, within the method, we connect with our database and bind the data. XML data can be submitted and the OnClick event calls the XMLBind() method as well. So as you see, apart from retrieval, we also have the submission of XML data.

A Few Last Words

Learning how to manipulate and control databases is easy and we intend to be there every step of the way to assist. Join us next time for additional database tutorials.

Download Source Files