Skip to content

Instantly share code, notes, and snippets.

@h09shais
Created August 23, 2018 10:02
Show Gist options
  • Save h09shais/411c8bd2fd373cbd619997b63f08c61a to your computer and use it in GitHub Desktop.
Save h09shais/411c8bd2fd373cbd619997b63f08c61a to your computer and use it in GitHub Desktop.
C# Model to XML
private static string CreateXml(object model)
{
var xmlDoc = new XmlDocument();
var xmlSerializer = new XmlSerializer(model.GetType());
using (var xmlStream = new MemoryStream())
{
xmlSerializer.Serialize(xmlStream, model);
xmlStream.Position = 0;
xmlDoc.Load(xmlStream);
return xmlDoc.InnerXml;
}
}
var model = ... ;
File.WriteAllText("Path, for example ... model.xml", CreateXml(model));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment