我有以下想要使用 xml 序列化重现的 XML:
<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>
以下是我的对象类型:
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();
}
}
当我尝试在 Room 对象中实例化另一个表对象时,出现错误(特别是对象重复)。我究竟做错了什么?
例如:
**Table = new Table{Type="Wood", Id="13"}**
如何在房间列表中实例化另一个表对象而不会出现重复错误