• How to bind XML file Data to GridView in ASP.NET

    by  • April 21, 2014 • Microsoft .NET C# (C-Sharp), Microsoft ASP.NET, Microsoft VB.NET • 0 Comments

    Sometimes we have data in XML format and we need to bind it to GridView control or sometimes we need to bind single tag values of XML file to DropDownList. This can be achieved in a very simple way. We just need to create an object of XmlTextReader and read XML file data in a DataSet object. Then we can simply bind this DataSet or a DataTable of DataSet to GridView. The most inner repeated tags works like columns and values between tags are fields. The outer repeated tags are like the table name. In case of DropDownList, we can simply set a tag as DataTextField and same tag or different tag as DataValueFiled. At the end we need to call the DataBind method of GridView or DropDownList as we normally do when we bind a GridView or DropDownList. Let’s see the code now.

     

    I have an XML file in my project as below with the name of Customers.xml

     

    Then I have a DropDownList control and a GridView control.

    Namespaces

    C#

    VB.NET

     

    Now see code below to read XML file data and bind it to DropDownList or GridView.

    C#

    VB.NET

     

    You can also download complete code sample from link below.

    Download199 downloads



    Namecheap.com
    Avatar of GetCodeSnippet.com

    About

    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    Leave a Reply