Tutorial RSS
 
Navigator: Home - Display - Display xml data using XMLDataSource and VB

Display xml data using XMLDataSource and VB

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

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!


Looking for the C#.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 Sub 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 ex As Exception
lblStatus.Text += ex.ToString()
End Try
End Sub

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.


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.

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub 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 ex As Exception
lblStatus.Text += ex.ToString()
End Try
End Sub

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
XMLBind()
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
XMLBind()
End Sub
End Class




Looking for the C#.NET 2005 Version? Click Here!

Looking for more DB Tutorials? Click Here!

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!


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