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
|
No comments:
Post a Comment