Skip to content

Instantly share code, notes, and snippets.

@dlidstrom
Created February 20, 2014 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlidstrom/9110140 to your computer and use it in GitHub Desktop.
Save dlidstrom/9110140 to your computer and use it in GitHub Desktop.
Serialize into xml
var serializer = new XmlSerializer(command.GetType());
MemoryStream stream = null;
try
{
stream = new MemoryStream();
var streamRef = stream;
using (var writer = new XmlTextWriter(stream, Encoding.Unicode))
{
// stream is owned by writer and will be disposed automatically
stream = null;
serializer.Serialize(writer, command);
var xml = Encoding.Unicode.GetString(streamRef.ToArray());
var changeLog = new ChangeLog(principal, command.GetType(), xml);
context.Changes.Add(changeLog);
}
}
finally
{
if (stream != null) stream.Dispose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment