Wednesday, 27 July 2011

Displaying Data from XML file using ASP.NET and VB


This tutorial shows you how to display data that resides in an XML file, in a GridView and DataList. VB version.

This tutorial shows how XML pages can be used to store data, and how you can use ASP.NET to display that data on your web page.

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!

First, look at the XML file. You'll notice it's structure is hierarchical:

<?xml version="1.0" standalone="yes"?>
<videos>
  <video vidID="000000001"
      title="The Breakfast Club"
      price="9.95">
    <comments>
      <userComment rating="4"
          comment="Interesting watch, with laugh out loud moments." />
      <userComment rating="3"
          comment="Not bad." />
    </comments>
  </video>
  <video vidID="000000002"
       title="The Goonies"
       price="9.95">
    <comments>
      <userComment rating="5"
          comment="My kids loved it." />
      <userComment rating="2"
          comment="Childish. Inconceivable." />
    </comments>
  </video>
  <video vidID="000000003"
      title="The Lawnmower Man"
      price="10.95" >
    <comments>
      <userComment rating="4"
          comment="An interesting movie." />
    </comments>
  </video>
  <video vidID="000000004"
      title="War of the Worlds"
      price="16.95" >
    <comments>
      <userComment rating="4"
          comment="Excellent!" />
    </comments>
  </video>
  <video vidID="000000005"
      title="Titanic"
      price="1.95" >
  </video>
</videos>

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

To display the data, we use a GridView control and XmlDataSource.
In this example, there is also a DataList which displays data nested in the XML file.

<form id="form1" runat="server">
<div>
    <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Videos.xml">
    </asp:XmlDataSource>
    
</div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="XmlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None" DataKeyNames="vidID" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="vidID" HeaderText="ID" SortExpression="vidID" />
            <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" >
                <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="price" HeaderText="Price" SortExpression="price" />
        </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#EFF3FB" />
        <EditRowStyle BackColor="#2461BF" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    <asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource2" Visible="False">
        <ItemTemplate>
            User Rating:
            <asp:Label ID="ratingLabel" runat="server" Text='<%# Eval("rating") %>'></asp:Label><br />
            Comment:
            <asp:Label ID="commentLabel" runat="server" Text='<%# Eval("comment") %>'></asp:Label><br />
            <br />
        </ItemTemplate>
        <SeparatorTemplate>
            <hr />
        </SeparatorTemplate>
    </asp:DataList><br />
    <asp:XmlDataSource ID="XmlDataSource2" runat="server" DataFile="~/App_Data/Videos.xml"
        XPath="/videos/video/comments/userComment "></asp:XmlDataSource>
    &nbsp;
    <br />
</form>

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

The following code makes sure the DataList displays the nested elements in the XML file, which are not shown in the GridView, when the user selects one of the entries in the GridView. This dynamically changes the Xpath when the user selects a new item from the GridView.

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As ObjectByVal e As EventArgs)
    Dim vidID As String = CType(GridView1.DataKeys(GridView1.SelectedIndex).Value, String)
    XmlDataSource2.XPath = String.Format("/videos/video[@vidID='{0}']/comments/userComment", vidID)
    DataList1.Visible = True
End Sub

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

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment