<?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>
<!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>
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);
grdTable.DataSource=dt;
grdTable.DataBind();
}
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
dtable.Rows.Add(dr);
}
return
dtable;
}
}
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
grdTable.DataBind()
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)))
Next
' 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
Next
'add in
the values
dt.Rows.Add(dr)
Next
Return
dt
End Function
End Class
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
- C# Extension Methods Example
- How to generate random no in C#.Net
- ASP.Net Tutorials For Beginners
- Add httpcookie using c#.net
- Add query string value to hyperlink in C#.net/Asp.Net
- C# code to bind XML data to Grid view
- C# code to convert string to byte array and byte array to string in ASP.Net - New !!
- C# code to send mail using smtp from gmail,yahoo mail and live mail
- Check textbox is changed or not using javascript C#.net
- Check uncheck all checkboxes in grid view using jquery
Comments
Post a Comment