Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C#.Net- How to Convert DataTable to XML String in Asp.net, VB.NET

Dec 14, 2012
Introduction:

Here I will explain how to convert datatable to xml string in asp.net using C#, VB.NET.

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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

14 comments :

Anonymous said...

212121212123
11121
32132
321321
321321
231321231321

Ashutosh sharan Tripathi said...

Thanks

Unknown said...

how can i convert XML file into data table

Unknown said...

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

Anonymous said...

Thanks a lot.

Unknown said...

Thanks for the Method

Mention Datatable table name.Otherwise it won't work.

dt.TableName = "Country";

Unknown said...

Thanks

Unknown said...

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);
}

Anonymous said...

use Response.ContentType = "text/xml" to display the xml in its schematic format with all tags

gaurav said...

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

Unknown said...

Thank You So Much Sir

Unknown said...

Thanks Bro

Anonymous said...

Goods...........

Amandeep said...

Thank so much.......

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.