overview

serializing

When serializing a type with XmlSerializer, only public fields and properties are included, and the type must have a parameterless constructor.

List<Person> people =  // Create an object graph of Person objects in a List of Persons.
XmlSerializer xs = new(people.GetType()); // Create object that formats a List of Persons as XML.
using (FileStream stream = File.Create(path))
{
    xs.Serialize(stream, people); // Serialize the object graph to the stream.
}

deserializing

using (FileStream xmlLoad = File.Open(path, FileMode.Open))
{
    List<Person>? loadedPeople = xs.Deserialize(xmlLoad) as List<Person>; // Deserialize and cast object graph
}

decorators

  • [XmlAttribute("shortname")] — Use to specify a short name for properties.