Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to display XML file data in ASP.NET

Author: Mike Green
Download Source Code : 1349_DisplayXMLData.zip

I will explain in this article that how you can display XML file data in ASP.NET using GridView, DropDownList and CheckBoxList controls.

 

 

XML file data can be converted into DataSet and then you can bind this DataSet object to GridView, DropDownList or CheckBoxList.
 
  1. Open MS Visual Studio 2010
  2. File > New > Website > Visual C# or Visual Basic > ASP.NET Empty Web Site
  3. Select Web Location as File System and Click OK
  4. From Menu, Website > Add New Item > Select Web Form and Click Add.
  5. Drag and Drop a GridView control, a DropDownList Control and a CheckBoxList control.

    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    <br />
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
    <br />
    <asp:CheckBoxList ID="CheckBoxList1" runat="server">
    </asp:CheckBoxList>
     
  6. From Menu, Website > Add New Item > Select XML File and Click Add. Rename the XML File as Test.xml
     
  7. Open Test.xml and write code below in it

    <?xml version="1.0" encoding="utf-8" ?>
    <Countries>
           <Country>
                  <ID>1</ID>
                  <Name>United States</Name>
                  <Capital>Washington, DC</Capital>
                  <Currency>US Dollar</Currency>
                  <Continent>North America</Continent>
           </Country>
           <Country>
                  <ID>2</ID>
                  <Name>United Kingdom</Name>
                  <Capital>London</Capital>
                  <Currency>Pound Sterling</Currency>
                  <Continent>Europe</Continent>
           </Country>
           <Country>
                  <ID>3</ID>
                  <Name>Canada</Name>
                  <Capital>Ottawa</Capital>
                  <Currency>Canadian Dollar</Currency>
                  <Continent>North America</Continent>
           </Country>
           <Country>
                  <ID>4</ID>
                  <Name>France</Name>
                  <Capital>Paris</Capital>
                  <Currency>Euro</Currency>
                  <Continent>Europe</Continent>
           </Country>
    </Countries>
     
  8. Open code behind file and add following namespace in it

    C#
     
    using System.Data;

    VB.NET

    Imports System.Data
     
  9. Write code below in Page Load event.

    C#
     
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        string xmlfile = Server.MapPath("~/Test.xml");
        ds.ReadXml(xmlfile);
     
        GridView1.DataSource = ds;
        GridView1.DataBind();
     
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataBind();
     
        CheckBoxList1.DataSource = ds;
        CheckBoxList1.DataTextField = "Name";
        CheckBoxList1.DataValueField = "ID";
        CheckBoxList1.DataBind();
    }
     
    VB.NET
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ds As New DataSet()
        Dim xmlfile As String = Server.MapPath("~/Test.xml")
        ds.ReadXml(xmlfile)
     
        GridView1.DataSource = ds
        GridView1.DataBind()
     
        DropDownList1.DataSource = ds
        DropDownList1.DataTextField = "Name"
        DropDownList1.DataValueField = "ID"
        DropDownList1.DataBind()
     
        CheckBoxList1.DataSource = ds
        CheckBoxList1.DataTextField = "Name"
        CheckBoxList1.DataValueField = "ID"
        CheckBoxList1.DataBind()
    End Sub
     
    First we have created an object of DataSet. Then we have get the path of our XML file and call the ReadXml() method of DataSet object which will read the XML data of specified file into DataSet object. Now we can bind DataSet object to GridView, DropDownList and CheckBoxList controls. We have to set DataTextField and DataValueField properties in DropDownList and CheckBoxList controls. 

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to create and modify MS Word document in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net