// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; namespace E { public class Struktur { } [XmlRoot(ElementName="struktur1")] public class Struktur1 : Struktur { [XmlAttribute(AttributeName="a1")] public string A1 { get; set; } [XmlAttribute(AttributeName="a2")] public string A2 { get; set; } [XmlText] public string Content { get; set; } } [XmlRoot(ElementName="struktur2")] public class Struktur2 : Struktur { [XmlArray(ElementName="items")] [XmlArrayItem(ElementName = "item")] public List Items; } [XmlRoot(ElementName="root")] public class Root { [XmlElement(Type = typeof(Struktur1), ElementName = "struktur1")] [XmlElement(Type = typeof(Struktur2), ElementName = "struktur2")] public List Data; } public class Program { public static void Main(string[] args) { string xmlstr = @" Text 1 "; Console.WriteLine(xmlstr); XmlSerializer ser = new XmlSerializer(typeof(Root), new Type[] { typeof(Struktur1), typeof(Struktur2) }); //Root r = new Root() { Data = new List { new Struktur1() { A1 = "one", A2 = " two" , Content = "Text" }, new Struktur2() { Items = new List { 1, 2, 3 } } } }; //ser.Serialize(Console.Out, r); Root r = (Root)ser.Deserialize(new StringReader(xmlstr)); foreach(Struktur s in r.Data) { Console.WriteLine(s.GetType().FullName); } } } }