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.

Setup

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.

I just signed up at Server Intellect and couldn’t be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.

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.

Code Block
Default.aspx.vb

Protected Sub XMLBind()
Try

xmlDS.Data = txtXML.Text

xmlDS.DataBind()

rptXMLExample.DataSourceID = 
"xmlDS"

rptXMLExample.DataBind()
Catch ex As Exception
lblStatus.Text += ex.ToString()
End Try
End Sub

We chose Server Intellect for its cloud hosting, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

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.
Code Block
Default.aspx.

<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>


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: 

Code Block
Default.aspx.vb
Protected Sub btnSubmit_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles btnSubmit.Click
XMLBind()
End Sub
Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load
XMLBind()

End Sub

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.

We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud hosting!

XML_Display_Source_2010VB.zip