Skip to content

Instantly share code, notes, and snippets.

@AlexZeitler
Last active August 29, 2015 14:17
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 AlexZeitler/46b226b0ecc59619aa62 to your computer and use it in GitHub Desktop.
Save AlexZeitler/46b226b0ecc59619aa62 to your computer and use it in GitHub Desktop.
XML (de)serialization
class Program
{
static void Main(string[] args)
{
// serialize
using (var stream = new FileStream(".\\test.xml", FileMode.Create))
new XmlSerializer(typeof(Customer)).Serialize(
stream,
new Customer() {
Name = "PDMLab",
Id = 1,
Orders = new List<Order>() {
new Order() {
Id = 1,
Item = "MacBook Pro Retina"
}
}
});
Console.ReadLine();
// deserialize
using (var fileStream = new FileStream(".\\test.xml", FileMode.Open))
new XmlSerializer(typeof(Customer)).Deserialize(fileStream);
}
}
[Serializable]
public class Customer
{
public Customer()
{
Orders = new List<Order>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<Order> Orders { get; set; }
}
[Serializable]
public class Order
{
public int Id { get; set; }
public string Item { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment