C# XML Serialization of multiple objects of the same type within a single tag

Qrretbvffbtng

New Member
I have the following XML that I would like to reproduce using xml serialization:\[code\]<Room><!-- One light--> <light Type="Incadenscent" fin="QS f" ItemType="something "/><!-- Unlimited Tables --><table Type="BR" Id="10"/><table Type="BL" Id="21"/><table Type="BR" Id="22"/><table Type="GR" Id="35"/><table Type="BR" Id="18"/><table Type="RE" Id="55"/></Room>\[/code\]Below are my object types:\[code\]public class Table{ [XmlAttribute("type")] public string Type { get; set; } [XmlAttribute("Id")] public String Id { get; set; }}public class Light{ [XmlAttribute("type")] public string Type { get; set; } [XmlAttribute("fin")] public string FIN { get; set; } [XmlAttribute("ItemType")] public string ItemType { get; set; } }public class Room{ public Table Table { get; set; } public Light Light { get; set; } } public class Program{ static void Main(string[] args) { List<Room> list = new List<Room> { new Room { Light = new Light{ Type="Incadenscent", fin="QS", ItemType="something"}, Table = new Table{Type="Metal", Id="10"} //error here when I try to add a new table object Table = new Table{Type="Wood", Id="13"} } } ; SerializeToXML(list); } static public void SerializeToXML(List<Room> sample) { XmlSerializer serializer = new XmlSerializer(typeof(List<Room>));); TextWriter textWriter = new StreamWriter(@"C:\assets.xml"); serializer.Serialize(textWriter, sample); textWriter.Close(); }}\[/code\]I get an error(specifically-duplication of object) when I try to instantiate another table object within the Room object. What am I doing wrong?for example:\[code\] **Table = new Table{Type="Wood", Id="13"}**\[/code\]How can I instantiate another table object in the room list without getting a duplication error
 
Back
Top