Last active
August 29, 2015 14:17
-
-
Save AlexZeitler/46b226b0ecc59619aa62 to your computer and use it in GitHub Desktop.
XML (de)serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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