Tutorial by Examples

public void SerializeFoo(string fileName, Foo foo) { var serializer = new XmlSerializer(typeof(Foo)); using (var stream = File.Open(fileName, FileMode.Create)) { serializer.Serialize(stream, foo); } }
public Foo DeserializeFoo(string fileName) { var serializer = new XmlSerializer(typeof(Foo)); using (var stream = File.OpenRead(fileName)) { return (Foo)serializer.Deserialize(stream); } }
<Foo> <Dog/> </Foo> public class Foo { // Using XmlElement [XmlElement(Name="Dog")] public Animal Cat { get; set; } }
<Store> <Articles> <Product/> <Product/> </Articles> </Store> public class Store { [XmlArray("Articles")] public List<Product> Products {get; set; } }
public class Dog { private const string _birthStringFormat = "yyyy-MM-dd"; [XmlIgnore] public DateTime Birth {get; set;} [XmlElement(ElementName="Birth")] public string BirthString { get { return Birth.ToString(_birthStringFormat); } ...
Where we came from Sometimes we can't provide all of the required metadata needed for the XmlSerializer framework in attribute. Suppose we have a base class of serialized objects, and some of the derived classes are unknown to the base class. We can't place an attribute for all of the classes which...

Page 1 of 1