Reputation: 58863
I'm tring to get a string from a DataSet without using GetXml. I'm using WriteXml, instead. How to use it to get a string? Thanks
Upvotes: 41
Views: 57666
Reputation: 421968
string result = null;
using (TextWriter sw = new StringWriter())
{
dataSet.WriteXml(sw);
result = sw.ToString();
}
Upvotes: 71
Reputation: 1
public string ConvertDatatableToXML(DataTable dt)
MemoryStream str = new MemoryStream();
dt.WriteXml(str, true);
str.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(str);
string xmlstring = "";
xmlstring = sr.ReadToEnd();
return (xmlstring);
Upvotes: 0
Reputation: 5349
here is the vb.net code:
Private Function GenerateXML(ByVal ds As DataSet) As String
Dim obj As New StringWriter()
Dim xmlstring As String
ds.WriteXml(obj)
xmlstring = obj.ToString()
Return xmlstring
End Function
Upvotes: 1
Reputation: 1499770
Write to a StringWriter
, and then call ToString
on that.
Note that if you want the generated XML declaration to specify UTF-8 instead of UTF-16, you'll need something like my Utf8StringWriter
.
Upvotes: 9