ASP.Net – Convert XML to Datatable in C#.net, VB.Net


I have the below XML File 
<?xml version=1.0 encoding=utf-8?>
<root>
  <Employee>
    <Id>1</Id>
    <EmpName>Kannadasan</EmpName>
    <Gender>Male</Gender>
    <Dept>Reading Books</Dept>
  </Employee>
  <Employee>
    <Id>2</Id>
    <EmpName>Kaviyarasan</EmpName>
    <Gender>Male</Gender>
    <Dept>Playing Games</Dept>
  </Employee>
  <Employee>
    <Id>3</Id>
    <EmpName>Ramkumar</EmpName>
    <Gender>Male</Gender>
    <Dept>Reading Books</Dept>
  </Employee>
  <Employee>
    <Id>4</Id>
    <EmpName>Xavier Selvaraj</EmpName>
    <Gender>Male</Gender>
    <Dept>Playing Games</Dept>
  </Employee>
</root>
ASP.Net Code:
<!DOCTYPE html>
<html>
<head runat=”server”>
    <title>Convert XML to Data Table</title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
        <asp:GridView ID=”grdTable” runat=”server”>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
C#.Net Code

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Linq;
usingSystem.Xml.Linq;
usingSystem.Data;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(objectsender, EventArgs e)
    {
        XElementxele = XElement.Load(Server.MapPath(“test.xml”));//get your file
        // declare a new DataTable and pass your XElement to it
        DataTabledt = XElementToDataTable(xele);
        grdTable.DataSource=dt;
        grdTable.DataBind();
    }
    public DataTable XElementToDataTable(XElement x)
    {
        DataTabledtable = new DataTable();
        XElementsetup = (from p inx.Descendants() select p).First();
        // build your DataTable
        foreach(XElement xe insetup.Descendants())
            dtable.Columns.Add(new DataColumn(xe.Name.ToString(), typeof(string))); // add columns to your dt
        var all = from p inx.Descendants(setup.Name.ToString()) select p;
        foreach(XElement xe inall)
        {
            DataRowdr = dtable.NewRow();
            foreach(XElement xe2 inxe.Descendants())
                dr[xe2.Name.ToString()] = xe2.Value; //add in the values
            dtable.Rows.Add(dr);
        }
        returndtable;
    }
}

Below is the equivalent VB.Net

ImportsSystem.Collections.Generic
ImportsSystem.Web
ImportsSystem.Web.UI
ImportsSystem.Web.UI.WebControls
ImportsSystem.Linq
ImportsSystem.Xml.Linq
ImportsSystem.Data
Partial Public Class _Default
    InheritsSystem.Web.UI.Page
    Protected Sub Page_Load(sender AsObject, 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
        grdTable.DataBind()
    End Sub
    Public Function XElementToDataTable(x As XElement) As DataTable
        Dim dt As New DataTable()
                        Dimsetup As XElement= (From p Inx.Descendants()p).First()
        For Each xe As XElement Insetup.Descendants()
            ‘ build your DataTable
            dt.Columns.Add(New DataColumn(xe.Name.ToString(), GetType(String)))
        Next
        ‘ add columns to your dt
                        Dimall = From p Inx.Descendants(setup.Name.ToString())p
        For Each xe As XElement In all
            Dimdr As DataRow= dt.NewRow()
            ForEach xe2 As XElement Inxe.Descendants()
                dr(xe2.Name.ToString()) = xe2.Value
            Next
            ‘add in the values
            dt.Rows.Add(dr)
        Next
        Returndt
    End Function
End Class

The Output Will be:

Id EmpName Gender Dept
1 Kannadasan Male Reading Books
2 Kaviyarasan Male Playing Games
3 Ramkumar Male Reading Books
4 Xavier Selvaraj Male Playing 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 c#.net
  5. Add query string value to hyperlink in C#.net/Asp.Net
  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 C#.net
  10. Check uncheck all checkboxes in grid view using jquery

Comments

  1. By vitamin otak anak

    Reply

  2. Reply

Leave a Reply

Your email address will not be published. Required fields are marked *