Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataContractSerializer - how can I output the xml to a string (as opposed to a file)

I had a quick question regarding the datacontractserializer. Maybe it's more of a stream question. I found a piece of code that writes the xml to a filestream. I basically don't want the file and just need the string output.

public static string DataContractSerializeObject<T>(T objectToSerialize)
{   
    var fs = new FileStream("test.xml", FileMode.OpenOrCreate);
    var serializer = new DataContractSerializer(typeof(T));
    serializer.WriteObject(fs, objectToSerialize);
    fs.Close();
    return fs.ToString();
}

fs.ToString() is obviously not what I'm looking for. What stream or writer etc, can I use just to return the proper string and not create a file? I did look at the XML the filestream created and it's exactly what I'm looking for. The XmlSerializer wrote the XML a bit strange and I prefer the output of the DataContractSerializer in this case. Can anyone point me in the right direction?

like image 923
Hcabnettek Avatar asked Jul 13 '10 15:07

Hcabnettek


5 Answers

Something like this - put your output into a MemoryStream and then read that back in:

public static string DataContractSerializeObject<T>(T objectToSerialize)
{
    using(MemoryStream memStm = new MemoryStream())
    {
        var serializer = new DataContractSerializer(typeof(T));
        serializer.WriteObject(memStm, objectToSerialize);

        memStm.Seek(0, SeekOrigin.Begin);

        using(var streamReader = new StreamReader(memStm))
        {
             string result = streamReader.ReadToEnd();
             return result;
        }
    }
}
like image 122
marc_s Avatar answered Oct 03 '22 10:10

marc_s


Thanks to @xr280xr for pointing out my forgotten StringWriter disposal in the first draft.

/// <summary>
/// Converts this instance to XML.
/// </summary>
/// <returns>XML representing this instance.</returns>
public string ToXml()
{
    var serializer = new DataContractSerializer(this.GetType());
    using (var output = new StringWriter())
    using (var writer = new XmlTextWriter(output) { Formatting = Formatting.Indented })
    {
        serializer.WriteObject(writer, this);
        return output.GetStringBuilder().ToString();
    }
}
like image 24
Pat Avatar answered Oct 03 '22 10:10

Pat


And even easier:

var serializer = new DataContractSerializer(typeof(T));
var sb = new StringBuilder();
using (var writer = XmlWriter.Create(sb))
{
    serializer.WriteObject(writer, objectToSerialize);
    writer.Flush();
    return sb.ToString();
}
like image 28
root Avatar answered Oct 03 '22 10:10

root


I suggest combining the methods given by Pat and marc_s:

public static string DataContractSerializeObject<T>(T objectToSerialize)
{
    using (var output = new StringWriter())
    using (var writer = new XmlTextWriter(output) {Formatting = Formatting.Indented})
    {
        new DataContractSerializer(typeof (T)).WriteObject(writer, objectToSerialize);
        return output.GetStringBuilder().ToString();
    }
}
like image 26
Ohad Schneider Avatar answered Oct 03 '22 11:10

Ohad Schneider


A variant of @root's answer:

var serializer = new DataContractSerializer(typeof(T));
var sb = new StringBuilder();
using (var writer = XmlWriter.Create(sb))
{
    serializer.WriteObject(writer, objectToSerialize);
}

return sb.ToString();
like image 23
Johan Larsson Avatar answered Oct 03 '22 11:10

Johan Larsson