Wednesday, March 6, 2013

In this code example i'm explaining how to Read XML Data Into DataSet Using C# And VB.NET In Asp.Net.

Read Xml Data Into DataSet C# VB.NET Asp.Net


For this i have placed one GridView on the aspx page to display after reading Data from XML File on server in Click Event of Read Button.

XML File contains firstname, lastname and location sub elements under employee element. 



C# CODE
01using System.Data;
02using System.IO;
03protected void btnReadXmlFile_Click(object sender, EventArgs e)
04    {
05        string filePath = Server.MapPath("~/Employees.xml");
06        DataSet dsData = new DataSet();
07        //If you don't want to read through FileStream then comment below line
08        FileStream fsReadSchema = new FileStream(filePath, FileMode.Open);
09        dsData.ReadXml(fsReadSchema);
10        fsReadSchema.Close();
11        GridView1.DataSource = dsData;
12        GridView1.DataBind();
13    }

VB.NET CODE
01Protected Sub btnReadXmlFile_Click(sender As Object, e As EventArgs)
02 Dim filePath As String = Server.MapPath("~/Employees.xml")
03 Dim dsData As New DataSet()
04 'If you don't want to read through FileStream then comment below line
05 Dim fsReadSchema As New FileStream(filePath, FileMode.Open)
06 dsData.ReadXml(fsReadSchema)
07 fsReadSchema.Close()
08 GridView1.DataSource = dsData
09 GridView1.DataBind()
10End Sub

0 comments:

Post a Comment