This example uses a StringWriter
to hold the serialized data, then calling ToString()
gives the actual string
value:
Person john = new Person();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person));
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, john);
string serializedXML = stringWriter.ToString();
Is there any easier/Cleaner way to do this? All of the Serialize()
overloads seem to use a Stream
or Writer.
UPDATE: Asked a similar question about serializing an IEnumerable via an Extension Method .