到目前为止,我已经对其进行了调试,该列表具有值..但是它没有将它们写出到文件中。
我不确定为什么它是空的。
该类GameObjects
包含所有字段。
GameObjectData
只是为了列表。
然后ChestPlate
继承自GameObjects
. 这样做的原因是,我正在制作一个游戏,并且其中的所有值GameObjects
都与ChestPlate
.
代码如下:
[Serializable]
public class GameObjects
{
//Defines name within XML file:
[XmlElement("Item_ID")]
public int Item_ID { get; set; }
[XmlElement("Item_Name")]
public string Item_Name = "bob";
[XmlElement("Item_type")]
public string Item_type = "GameObject";
[XmlElement("Item_Level")]
public int Item_Level = 5;
[XmlElement("Item_description")]
public string Item_description = "best description evar";
public GameObjects(int id, string name, string type, int level, string description)
{
this.Item_ID = id;
this.Item_Name = name;
this.Item_type = type;
this.Item_Level = level;
this.Item_description = description;
}
}
[Serializable]
[XmlInclude(typeof(GameObjects))]
public class GameObjectData
{
[XmlArrayItem(typeof(GameObjects))]
public List<GameObjects> GameList { get; set;}
}
public class ChestPlate : GameObjects
{
[XmlElement("Armour_Rating")]
int Armour_Rating = 5;
public ChestPlate(int Armour_id, string Armour_name, string Armour_type, int Armour_level, string Armour_description)
: base(Armour_id, Armour_name, Armour_type, Armour_level, Armour_description)
{
this.Item_ID = Armour_id;
this.Item_Name = Armour_name;
this.Item_type = Armour_type;
this.Item_Level = Armour_level;
this.Item_description = Armour_description;
}
public void SerializeToXML(List<GameObjects> responsedata)
{
GameObjectData f = new GameObjectData();
f.GameList = new List<GameObjects>();
f.GameList.Add(new GameObjects { Item_ID = 1234, Item_Name = "OMG", Item_type = "CHESTPLATE", Item_Level = 5, Item_description = "omg" });
XmlSerializer serializer = new XmlSerializer(typeof(GameObjectData));
TextWriter textWriter = new StreamWriter(@"C:\Test.xml");
serializer.Serialize(textWriter, f);
Console.WriteLine(f);
}
class Program
{
static void Main(string[] args)
{
GameObjects a = new GameObjects();
ChestPlate b = new ChestPlate();
List<GameObjects> d = new List<GameObjects>();
b.SerializeToXML(d);
Console.ReadLine();
}
}