Introduction:
Description:
In
previous posts I explained asp.net, C#, SQL Server Interview Questions, send gridview as email body, upload and download files from gridview and many articles
relating to asp.net,
C#,
VB.NET
code snippets. Now I will explain
how to convert datatable to xml
string in asp.net using C#,
VB.NET.
If
you want to convert datable to xml you need to write a code as shown below
C#
Code
// By using this method we can
convert datatable to xml
public string
ConvertDatatableToXML(DataTable dt)
{
MemoryStream str = new MemoryStream();
dt.WriteXml(str, true);
str.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(str);
string xmlstr;
xmlstr = sr.ReadToEnd();
return (xmlstr);
}
|
VB.NET
Code
' By using this method we can
convert datatable to xml
Public Function
ConvertDatatableToXML(ByVal dt As DataTable) As String
Dim str As New MemoryStream()
dt.WriteXml(str, True)
str.Seek(0, SeekOrigin.Begin)
Dim sr As New StreamReader(str)
Dim xmlstr As String
xmlstr = sr.ReadToEnd()
Return (xmlstr)
End Function
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
14 comments :
212121212123
11121
32132
321321
321321
231321231321
Thanks
how can i convert XML file into data table
Hi,
How can we convert a XML file to EXCEL/WORD DOC ??
And same EXCEL/WORD doc to XML again without using any open source dll's ??
Please reply ASAP
Thanks a lot.
Thanks for the Method
Mention Datatable table name.Otherwise it won't work.
dt.TableName = "Country";
Thanks
Hi It's Work For me Thank You . I'm Beginner So I'm Confused How To Use This Code At Starting . So I Just Add Full Code In This Comment . May Be It Help Person Like Me
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == true)
return;
DataTable dtTemp = new DataTable();
dtTemp.TableName = "Test";
dtTemp.Columns.Add("User ID");
dtTemp.Columns.Add("Name");
dtTemp.Columns.Add("City");
dtTemp.Columns.Add("Address");
dtTemp.Rows.Add("005", "one", "Onecity", 1111);
dtTemp.Rows.Add("002","Two","TwoCity",2222);
dtTemp.Rows.Add("003", "Three", "ThreeCity", 333);
ConvertDatatableToXML(dtTemp);
}
public string ConvertDatatableToXML(DataTable dtTemp)
{
MemoryStream str = new MemoryStream();
dtTemp.WriteXml(str, true);
str.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(str);
string xmlstr;
xmlstr = sr.ReadToEnd();
return (xmlstr);
}
use Response.ContentType = "text/xml" to display the xml in its schematic format with all tags
Code is giving me stack overflow exception for 4 lakh records in data table
While executing the line "xmlstr = sr.ReadToEnd();"
Is there any alternative way to handle such a bulk record
Thank You So Much Sir
Thanks Bro
Goods...........
Thank so much.......
Note: Only a member of this blog may post a comment.