Quantcast
Channel: Serialize an object to string - Stack Overflow
Viewing all articles
Browse latest Browse all 12

Serialize an object to string

$
0
0

I have the following method to save an Object to a file:

// Save an object out to the diskpublic static void SerializeObject<T>(this T toSerialize, String filename){    XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());    TextWriter textWriter = new StreamWriter(filename);    xmlSerializer.Serialize(textWriter, toSerialize);    textWriter.Close();}

I confess I did not write it (I only converted it to a extension method that took a type parameter).

Now I need it to give the xml back to me as a string (rather than save it to a file). I am looking into it, but I have not figured it out yet.

I thought this might be really easy for someone familiar with these objects. If not I will figure it out eventually.


Viewing all articles
Browse latest Browse all 12

Latest Images

Trending Articles





Latest Images