Load XML File Into ASP.NET Gridview


asp.net gridview load xml file

In this articles i'm going to explain about How to Load XML File into Gridview by Using C# Coding in ASP.NET. Article Posted by Ramullu.

  1. Xml is used to describe the data.
  2. Xml cant be used for designing
  3. Xml is a case sensitive programming language
  4. Xml does not contain any predefined tags
  5. All xml tags are predefined
  6. Xml is open standard programming language i.e any technology can understand xml format
  7. Xml stores the data in hierarchal form.
  8. Xml supports simple and complex datatypes
  9. Xml code can be written in any text editors.
  10. Xml code can be executed with in the browser or in any other application which contains xml parser.
asp.net gridview load xml file Xml follows hierarchical manner this structure is also known as DOM Document Object Model
  • Any xml file contain only one root tag
  • One or more parent tags and Every parent tag contains one or more child tags
asp.net gridview load xml file

<Roottag>
 <ParentTag1>
  <ChildTag1>Data</ChildTag1>
  <ChildTag2>Data</ChildTag2>
  <ChildTag3>Data</ChildTag3>
 </ParentTag1>
 <ParentTag2>
  <ChildTag1>Data</ChildTag1>
  <ChildTag2>Data</ChildTag2>
  <ChildTag3>Data</ChildTag3>
 </ParentTag2>
</Roottag>

Open your Visual Studio -> Right Click on Project -> Add New Item -> Select - XML File and Choose name Employees.xml


<?xml version="1.0" encoding="utf-8"?>
<Employee>
 <EmpDetails>
  <Empid>101</Empid>
  <EmpName>kanna</EmpName>
  <Designation>dev</Designation>
  <Doj>1/2/2010</Doj>
  <salary>35000</salary>
  <deptno>20</deptno>
 </EmpDetails>

 <EmpDetails>
  <Empid>102</Empid>
  <EmpName>anil</EmpName>
  <Designation>dev</Designation>
  <Doj>1/2/2010</Doj>
  <salary>25000</salary>
  <deptno>20</deptno>
 </EmpDetails>

 <EmpDetails>
  <Empid>103</Empid>
  <EmpName>sunil</EmpName>
  <Designation>dev</Designation>
  <Doj>1/2/2010</Doj>
  <salary>35000</salary>
  <deptno>20</deptno>
 </EmpDetails>
</Employee>

Microsoft is giving two separate namespace to code or work with xml files.

 using System.xml
 using System.Data

Creating an xml file in visual studio editor

Reading and writing xml files in .NET.

asp.net gridview load xml file
  • xmlTextReaderClass : his class is used to Read the data from an XML file
  • Read() : this method is used to read the data using element by element.
  • Read() : is used to read the data in sequential form.
  • Read method returns true if it encounters any data.
  • Close() : this method is used to close an XML file that has been opened for Reading

Properties of XmlTextReader Class

  • Name : this property is used to store the tag name the tag might be opening tag or closing tag.
  • Value : this property stores the value that present in child tags.
  • NodeType : is used to identify the type of the node in xmlDom whether it belongs to comments/elements/endelements/text/whitespace.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("Employees.xml"));
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}
0 Komentar untuk "Load XML File Into ASP.NET Gridview"

Back To Top