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

How to display XML file data in ASP.NET

Author: Mike Green
Download Source Code :

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">
    <br />
    <asp:DropDownList ID="DropDownList1" runat="server">
    <br />
    <asp:CheckBoxList ID="CheckBoxList1" runat="server">
  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" ?>
                  <Name>United States</Name>
                  <Capital>Washington, DC</Capital>
                  <Currency>US Dollar</Currency>
                  <Continent>North America</Continent>
                  <Name>United Kingdom</Name>
                  <Currency>Pound Sterling</Currency>
                  <Currency>Canadian Dollar</Currency>
                  <Continent>North America</Continent>
  8. Open code behind file and add following namespace in it

    using System.Data;


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

    protected void Page_Load(object sender, EventArgs e)
        DataSet ds = new DataSet();
        string xmlfile = Server.MapPath("~/Test.xml");
        GridView1.DataSource = ds;
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "ID";
        CheckBoxList1.DataSource = ds;
        CheckBoxList1.DataTextField = "Name";
        CheckBoxList1.DataValueField = "ID";
    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")
        GridView1.DataSource = ds
        DropDownList1.DataSource = ds
        DropDownList1.DataTextField = "Name"
        DropDownList1.DataValueField = "ID"
        CheckBoxList1.DataSource = ds
        CheckBoxList1.DataTextField = "Name"
        CheckBoxList1.DataValueField = "ID"
    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