pistacchio
pistacchio

Reputation: 58863

DataSet.WriteXml to string

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

Answers (4)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421968

string result = null;

using (TextWriter sw = new StringWriter())
{
    dataSet.WriteXml(sw);
    result = sw.ToString();
}

Upvotes: 71

Mehul Patel
Mehul Patel

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

DareDevil
DareDevil

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

Jon Skeet
Jon Skeet

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

Related Questions