在列表中派生类的XML序列化,需要在父类上设置成员

时间:2015-05-13 02:22:04

标签: c# xml serialization xna xml-serialization

作为一个爱好,我在XNA和C#中制作2D RPG。我有一个名为ObjectiveData的类。它唯一的成员是一个名为objectiveID的字符串。由此得出的是各种其他类(GatherItemsObjectiveData,SpeakToNPCObjectiveData等) - 所有这些类都假设存储与该特定目标相关的自己的客观数据(gatherItemsObjectiveData将保存关于要收集哪个项目以及有多少等的详细信息)。它们之间唯一的共同点是objectiveID,因此它在父类中。

当每个派生类都有自己的objectiveID变量时,我有这个工作的XML序列化,但是我希望它在父类中,这样我就可以在不知道它是什么派生类的情况下得到objectiveID等。我切换到将parentID放在父级中,它不再起作用(说XMl元素“目标”未找到)。所以现在我想弄清楚如何让它将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。

1 个答案:

答案 0 :(得分:1)

根据Everything you ever wanted to know about IntermediateSerializer

  

基类成员在派生类型

的数据之前被序列化

因此,您需要将<objectiveID>元素移动到预先存在的XML文件中包含元素的开头。