[英]XML serialization of derived class in a list, need to set member on parent class
作为一种业余爱好,我正在XNA和C#中制作2D RPG。 我有一个叫做ObjectiveData的类。 它的唯一成员是一个名为ObjectiveID的字符串。 从中派生出各种其他类(GatherItemsObjectiveData,SpeakToNPCObjectiveData等)-它们都假定存储与该特定目标相关的自己的目标数据(gatherItemsObjectiveData将保存有关收集哪个项目以及多少个项目的详细信息)。 它们之间唯一的共同点是ObjectiveID,因此它为什么在父类中。
当每个派生类都有其自己的objectiveID变量时,我已经对该工作进行了XML序列化,但是我希望它位于父类中,这样我就可以在不知道它是什么派生类的情况下获得objectiveID,等等。切换到将objectID放在父对象中后,它不再起作用(例如未找到XMl元素“ objective”)。 因此,现在我试图弄清楚如何使它将ObjectiveID传递给XML文件中的其父对象。
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace RpgLibrary.QuestClasses
{
public class ObjectivesData
{
[XmlArray("ObjectivesList")]
[XmlArrayItem("KillXObjectiveData", typeof(KillXObjectiveData))]
[XmlArrayItem("GatherXItemsObjectiveData", typeof(GatherXItemsObjectiveData))]
[XmlArrayItem("SpeakToNPCObjectiveData", typeof(SpeakToNPCObjectiveData))]
[XmlArrayItem("VisitAreaObjectiveData", typeof(VisitAreaObjectiveData))]
public List<ObjectiveData> ObjectivesList;
public ObjectivesData()
{
ObjectivesList = new List<ObjectiveData>();
}
}
[XmlInclude(typeof(KillXObjectiveData))]
[XmlInclude(typeof(GatherXItemsObjectiveData))]
[XmlInclude(typeof(SpeakToNPCObjectiveData))]
[XmlInclude(typeof(VisitAreaObjectiveData))]
public class ObjectiveData
{
public string objectiveID;
public ObjectiveData()
{
}
public ObjectiveData(string objectiveID)
{
this.objectiveID = objectiveID;
}
}
[XmlType(TypeName = "KillXObjectiveData")]
public class KillXObjectiveData : ObjectiveData
{
public string killTotal;
public string npcID;
public KillXObjectiveData()
{
}
public KillXObjectiveData(string killTotal, string npcID, string objectiveID)
: base(objectiveID)
{
this.killTotal = killTotal;
this.npcID = npcID;
}
}
[XmlType(TypeName = "GatherXItemsObjectiveData")]
public class GatherXItemsObjectiveData : ObjectiveData
{
public string gatherTotal;
public string itemID;
public GatherXItemsObjectiveData()
{
}
public GatherXItemsObjectiveData(string gatherTotal, string itemID, string objectiveID)
: base(objectiveID)
{
this.gatherTotal = gatherTotal;
this.itemID = itemID;
}
}
[XmlType(TypeName = "SpeakToNPCObjectiveData")]
public class SpeakToNPCObjectiveData : ObjectiveData
{
public string npcID;
public SpeakToNPCObjectiveData()
{
}
public SpeakToNPCObjectiveData(string npcID, string objectiveID)
: base(objectiveID)
{
this.npcID = npcID;
}
}
[XmlType(TypeName = "VisitAreaObjectiveData")]
public class VisitAreaObjectiveData : ObjectiveData
{
public string areaID;
public VisitAreaObjectiveData()
{
}
public VisitAreaObjectiveData(string areaID, string objectiveID)
: base(objectiveID)
{
this.areaID = areaID;
}
}
}
XML档案:
<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:QuestClasses="RpgLibrary.QuestClasses">
<Asset Type="QuestClasses:ObjectivesData">
<ObjectivesList>
<Item Type="QuestClasses:GatherXItemsObjectiveData">
<gatherTotal>5</gatherTotal>
<itemID>1</itemID>
<objectiveID>1</objectiveID>
</Item>
<Item Type="QuestClasses:KillXObjectiveData">
<killTotal>5</killTotal>
<npcID>900</npcID>
<objectiveID>2</objectiveID>
</Item>
</ObjectivesList>
</Asset>
</XnaContent>
最终结果应该是,我获得了存储有所有派生类对象的ObjectiveData列表,而我只需要在父类中正确设置ObjectiveID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.