Skip to content

Instantly share code, notes, and snippets.

@gustavodaquino
Created December 18, 2017 19:10
Show Gist options
  • Save gustavodaquino/bde9faeae90085d6d39f3eb5fb8c7bff to your computer and use it in GitHub Desktop.
Save gustavodaquino/bde9faeae90085d6d39f3eb5fb8c7bff to your computer and use it in GitHub Desktop.
Serializing a C# class to XML
[Serializable]
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
var serializer = new XmlSerializer(typeof(Person));
string xml;
using (var stringWriter = new StringWriter())
{
serializer.Serialize(stringWriter, new Person { FirstName = "John", LastName = "Doe", Age = 42 });
xml = stringWriter.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment