Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Create XML document in C# using LINQ XDocument Class

In this articles, I am going to explain you how to create XML document in C# using LINQ XDocument class. Below is step by step tutorial.

Namespace Required

Create an empty asp.net web application and add a webform to it.
Include below namespace.

How to bind GridView control from xml file in asp.net with C#

In this article, I will explain you how to bind GridView control from a XML file in asp.net with C#.

Creating Data Source

First, we need to create a xml file named as StudentDetails.xml. To create a XML file.
Go to: website >right click >add new item >go to data tab >select xml file >save file named as StudentDetails.xml at root directory.

Below is the xml file containing the Student details.

<?xml version="1.0" encoding="utf-8" ?>
<StudentDetails>
  <Student>
    <StudentId>1</StudentId>
    <Name>Rahul</Name>
    <Mark>80</Mark>
</Student>
  <Student>
    <StudentId>2</StudentId>
    <Name>John</Name>
    <Mark>85</Mark>
  </Student>
</StudentDetails>

In Designer File

In designer file, create a GridView control named as grdStudent.

 <div align="center">
  <asp:GridView ID="grdStudent" runat="server"> <HeaderStyle BackColor="#00A6DC" ForeColor="White"/>
  </asp:GridView>
 </div>

Namespace Used

Include the below Namespace.

using System.Data;

Complete C# Code

ReadXml() method will read data into a DataSet from specified XML file.

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("~/StudentDetails.xml"));
            grdStudent.DataSource = ds.Tables[0];
            grdStudent.DataBind();
        }

Output

Output will be shown as below.

Bind GridView from a xml file