我有下面的XML,我想使用XML序列化重現:C#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"}**
我如何可以實例在房間列表中的其他表對象沒有得到一個重複錯誤
每個房間都有一盞燈和一張桌子,因此,要添加另一張桌子,您需要另一個房間。 –
如何在房間列表中實例化另一個表格對象而不會出現重複錯誤? – user1324418