Loading XML Files into GridView Using C# Coding

Introduction:

Visualize the power of XML in ASP.NET as we delve into the intricacies of loading XML files into GridView. This comprehensive guide, accompanied by C# coding examples, empowers you to harness XML's potential for efficient data handling in your ASP.NET projects.

asp.net gridview load xml file

Understanding XML:

  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

Hierarchical Structure:

Experience the elegance of XML's hierarchical structure through practical examples. Learn how each XML file contains a root tag, parent tags, and child tags, creating a well-organized and easily navigable data structure.

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>

Creating an XML File in Visual Studio:

Step into Visual Studio and witness the creation of an XML file. Follow the simple steps: Right-click on your project, add a new XML file, and provide a name. The article guides you through creating an "Employees.xml" file for practical implementation.


<?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>

Reading XML in .NET:

Embark on the journey of reading and writing XML files in .NET. Discover the XmlTextReader class, a powerful tool for reading data from XML files. Learn about its methods like Read() and properties like Name, Value, and NodeType, unlocking the potential to extract valuable information.


 using System.xml
 using System.Data

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.

Practical Implementation:

The article seamlessly integrates theory with practice by providing a C# code snippet showcasing the use of XmlTextReader to read an XML file. The code, demonstrated in a sample ASP.NET page, loads data from "Employees.xml" into a GridView, creating a dynamic and user-friendly display.


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();
    }
}

Conclusion:

Master the art of loading XML files into GridView with this step-by-step guide. Empower your ASP.NET projects with the efficiency and flexibility of XML, transforming your data handling capabilities. Elevate your development skills and create dynamic, data-driven web applications effortlessly.

Post a Comment

0 Comments