ASP.Net - Convert XML to Datatable in, VB.Net


I have the below XML File 

<?xml version="1.0" encoding="utf-8"?>
    <Dept>Reading Books</Dept>
    <Dept>Playing Games</Dept>
    <Dept>Reading Books</Dept>
    <EmpName>Xavier Selvaraj</EmpName>
    <Dept>Playing Games</Dept>

ASP.Net Code:

<!DOCTYPE html>
<head runat="server">
    <title>Convert XML to Data Table</title>
    <form id="form1" runat="server">
        <asp:GridView ID="grdTable" runat="server">

C#.Net Code

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using System.Xml.Linq;
using System.Data;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        XElement xele = XElement.Load(Server.MapPath("test.xml"));//get your file
        // declare a new DataTable and pass your XElement to it
        DataTable dt = XElementToDataTable(xele);
    public DataTable XElementToDataTable(XElement x)
        DataTable dtable = new DataTable();

        XElement setup = (from p in x.Descendants() select p).First();
        // build your DataTable
        foreach (XElement xe in setup.Descendants())
            dtable.Columns.Add(new DataColumn(xe.Name.ToString(), typeof(string))); // add columns to your dt

        var all = from p in x.Descendants(setup.Name.ToString()) select p;
        foreach (XElement xe in all)
            DataRow dr = dtable.NewRow();
            foreach (XElement xe2 in xe.Descendants())
                dr[xe2.Name.ToString()] = xe2.Value; //add in the values
        return dtable;

Below is the equivalent VB.Net

Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Linq
Imports System.Xml.Linq
Imports System.Data

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        Dim x As XElement = XElement.Load(Server.MapPath("test.xml"))
        'get your file
        ' declare a new DataTable and pass your XElement to it
        Dim dt As DataTable = XElementToDataTable(x)
        grdTable.DataSource = dt
    End Sub
    Public Function XElementToDataTable(x As XElement) As DataTable
        Dim dt As New DataTable()

                        Dim setup As XElement = (From p In x.Descendants()p).First()
        For Each xe As XElement In setup.Descendants()
            ' build your DataTable
            dt.Columns.Add(New DataColumn(xe.Name.ToString(), GetType(String)))
        ' add columns to your dt
                        Dim all = From p In x.Descendants(setup.Name.ToString())p
        For Each xe As XElement In all
            Dim dr As DataRow = dt.NewRow()
            For Each xe2 As XElement In xe.Descendants()
                dr(xe2.Name.ToString()) = xe2.Value
            'add in the values
        Return dt
    End Function
End Class

The Output Will be:

1KannadasanMaleReading Books
2KaviyarasanMalePlaying Games
3RamkumarMaleReading Books
4Xavier SelvarajMalePlaying Games

You may also like

  1. C# Extension Methods Example
  2. How to generate random no in C#.Net
  3. ASP.Net Tutorials For Beginners
  4. Add httpcookie using
  5. Add query string value to hyperlink in
  6. C# code to bind XML data to Grid view
  7. C# code to convert string to byte array and byte array to string in ASP.Net - New !!
  8. C# code to send mail using smtp from gmail,yahoo mail and live mail
  9. Check textbox is changed or not using javascript
  10. Check uncheck all checkboxes in grid view using jquery


Post a Comment

Online Casino