Read XML Data Into DataSet In Asp.Net C# VB.NET

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
using System.Data;
using System.IO;
protected void btnReadXmlFile_Click(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("~/Employees.xml");
        DataSet dsData = new DataSet();
        //If you don't want to read through FileStream then comment below line
        FileStream fsReadSchema = new FileStream(filePath, FileMode.Open);
        dsData.ReadXml(fsReadSchema);
        fsReadSchema.Close();
        GridView1.DataSource = dsData;
        GridView1.DataBind();
    }

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

Build and run the code.

If you like this post than join us or share

1 comments:

Kees said...

Why don't you use dsData.ReadXml(filePath); ?


Find More Articles