Display Data with an XMLDataSource Control in VB
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 .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.
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.
Protected Sub XMLBind()
xmlDS.Data = txtXML.Text
rptXMLExample.DataSourceID = "xmlDS"
Catch ex As Exception
lblStatus.Text += ex.ToString()
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.
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.
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<td align="right" bgcolor="#eeeeee" class="header1">Repeater Control:</td>
<asp:repeater ID="rptXMLExample" runat="server">
<strong><%# XPath("@name") %></strong>
<br /><%# XPath("desc") %><br />
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> XML:</td>
<asp:textbox ID="txtXML" runat="server" Rows="10" TextMode="MultiLine" Width="465px"><?xml version="1.0" encoding="utf-8" ?>
<desc>The member or type is fully accessible.</desc>
<desc>The member or type is accessible from within its assembly</desc>
<desc>The member or type is only accessible from within the enclosing type</desc>
<desc>The member or type is only accessible from within the enclosing class or a class that inherits from the enclosing class</desc>
<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>
<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>
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:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
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.