How to Display XML Data using ASP.NET 2.0 and VB .NET
This tutorial will show you how to display XML Data using the XMLDataSource control, ASP.NET 2.0, and VB.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.
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 |
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"><?xml version="1.0" encoding="utf-8" ?> <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 |