将xml反序列化为对象列表

时间:2017-11-09 18:42:23

标签: c# xml

我试图将一些xml反序列化为一个对象列表。

我遇到麻烦的地方是Resources对象中的辅助对象Event没有填充值。对于更多的背景,这是一个统一项目,我反序列化可能发生的随机事件列表。

以下是我正在测试的XML:

<?xml version='1.0'?>
<Events>
  <Event>
    <Description>
      this is a test
    </Description>
    <Reward>
        <Food>
          0
        </Food>
        <Energy>
          1
        </Energy>
        <Happiness>
          2
        </Happiness>
        <ShipHp>
          3
        </ShipHp>
        <Garbage>
          4
        </Garbage>
    </Reward>
    <Cost>
        <Food>
          0
        </Food>
        <Energy>
          1
        </Energy>
        <Happiness>
          2
        </Happiness>
        <ShipHp>
          3
        </ShipHp>
        <Garbage>
          4
        </Garbage>
    </Cost>
    <EventType>
      Good
    </EventType>
    <EventId>
      1
    </EventId>
  </Event>
  <Event>
    <Description>
      this is a test
    </Description>
    <Reward>
        <Food>
          0
        </Food>
        <Energy>
          1
        </Energy>
        <Happiness>
          2
        </Happiness>
        <ShipHp>
          3
        </ShipHp>
        <Garbage>
          4
        </Garbage>
    </Reward>
    <Cost>
        <Food>
          0
        </Food>
        <Energy>
          1
        </Energy>
        <Happiness>
          2
        </Happiness>
        <ShipHp>
          3
        </ShipHp>
        <Garbage>
          4
        </Garbage>
    </Cost>
    <EventType>
      Good
    </EventType>
    <EventId>
      1
    </EventId>
  </Event>
</Events>

以下是对象(事件和资源):

public class Event {
    [XmlElement("Description")]
    public string Description { get; set; }

    [XmlElement("Reward")]
    public Resources Reward { get; set; }

    [XmlElement("Cost")]
    public Resources Cost { get; set; }

    [XmlElement("EventId")]
    public int EventId { get; set; }

    [XmlElement("EventType")]
    public string EventType { get; set; }

}

[XmlType("Resources")]
public class Resources {

    //Each prop can be extended to food being a Food object with expiry, etc
    [XmlElement("Food")]
    public int Food { get; set; } // 0 to cap
    [XmlElement("Happiness")]
    public int Happiness { get; set; } // 0 to 100
    [XmlElement("Energy")]
    public int Energy { get; set; } //0 to cap
    [XmlElement("ShipHp")]
    public int ShipHp { get; set; }// cap to 0
    [XmlElement("Garbage")]
    public int Garbage { get; set; } // 0 to cap
}

这是我的反序列化代码:

   public class EventHandler : MonoBehaviour
{

    [XmlInclude(typeof(Event))]
    [XmlInclude(typeof(Resources))]
    [Serializable, XmlRoot("Events")]
    public class EventData
    {
        [XmlElement("Event")]
        public List<Event> Events { get; set; }

        public EventData()
        {
            List<Event> Events = new List<Event>();
        }
    }

    List<Event> Events = new List<Event>();
    public static EventHandler instance;

    // Use this for initialization, called on script enabling
    void Start()
    {

        var serializer = new XmlSerializer(typeof(EventData));
        try
        {
            string xml = File.ReadAllText("Assets/TextResources/Events.xml");
            xml = xml.Replace("\n", string.Empty);
            xml = xml.Replace("\r", string.Empty);
            xml = xml.Replace("\t", string.Empty);
            using (var stringReader = new StringReader(xml))
            {
                using (var reader = XmlReader.Create(stringReader))
                {
                    var result = (EventData)serializer.Deserialize(reader);
                    Events = result.Events;

                }
            }
        }
        catch(Exception ex)
        {
            Debug.Log(ex);
        }
    }
}

因此,从此代码中,我的事件对象将填充DescriptionEventTypeEventId的值,但RewardCost资源对象没有填充价值观,到目前为止,我不知道为什么会这样。对于奖励积分,我也在努力寻找一种方法来简单地删除我从解析字符串中获得的所有死空间。 (即Good改为&#34; \ n \ r \ n好&#34;)。

2 个答案:

答案 0 :(得分:1)

行。所以我找到了问题的解决方案,我没有这方面的任何专家,如果有任何更正,更有经验的人想要添加以改进代码,我会很乐意做出改变。

首先,我的XML存在格式问题,为了找到这个,我实际上序列化了一个事件对象列表,并使用生成的文本找到了解决方案,下面是修订后的文本:

<ArrayOfEvent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Event>
    <Description>abc</Description>
    <Reward>
      <Food>0</Food>
      <Happiness>2</Happiness>
      <Energy>1</Energy>
      <ShipHp>3</ShipHp>
      <Garbage>4</Garbage>
    </Reward>
    <Cost>
      <Food>0</Food>
      <Happiness>2</Happiness>
      <Energy>1</Energy>
      <ShipHp>3</ShipHp>
      <Garbage>4</Garbage>
    </Cost>
    <EventId>1</EventId>
    <EventType>Good</EventType>
  </Event>
  <Event>
    <Description>def</Description>
    <Reward>
      <Food>0</Food>
      <Happiness>2</Happiness>
      <Energy>1</Energy>
      <ShipHp>3</ShipHp>
      <Garbage>4</Garbage>
    </Reward>
    <Cost>
      <Food>0</Food>
      <Happiness>2</Happiness>
      <Energy>1</Energy>
      <ShipHp>3</ShipHp>
      <Garbage>4</Garbage>
    </Cost>
    <EventId>2</EventId>
    <EventType>Good</EventType>
  </Event>
</ArrayOfEvent>

首先要注意的是我序列化了一个事件对象列表,这是<ArrayOfEvent>标记和生成的xml Name Space属性。从那以后。这也意味着我必须从EventData类更改为List<Event>,这在任何方面都更方便。从我的EventResources类开始,我将[Serializable, XmlRoot()]标记添加到其类中。 另外需要注意的是,我将EventType从字符串交换为枚举,该枚举的代码也将在下面提供 这些修复程序还修复了我从xml纯文本到XmlReader的格式问题 我将为以后的读者分享以下完整的代码:

[Serializable, XmlRoot("Event")]
public class Event
{
    [XmlElement("Description")]
    public string Description { get; set; }

    [XmlElement("Reward")]
    public Resources Reward { get; set; }

    [XmlElement("Cost")]
    public Resources Cost { get; set; }

    [XmlElement("EventId")]
    public int EventId { get; set; }

    [XmlElement("EventType")]
    public EventType EventType { get; set; }
    [XmlIgnore]
    public string MarkUp { get; set; }
}


[Serializable, XmlRoot("Resources")]
public class Resources
{
    //Each prop can be extended to food being a Food object with expiry, etc
    [XmlElement("Food")]
    public int Food { get; set; } // 0 to cap
    [XmlElement("Happiness")]
    public int Happiness { get; set; } // 0 to 100
    [XmlElement("Energy")]
    public int Energy { get; set; } //0 to cap
    [XmlElement("ShipHp")]
    public int ShipHp { get; set; }// cap to 0
    [XmlElement("Garbage")]
    public int Garbage { get; set; } // 0 to cap
}

[XmlType("EventType")]
public enum EventType
{
    [XmlEnum("Good")]
    Good = 0,
    [XmlEnum("Bad")]
    Bad,
    [XmlEnum("Neutral")]
    Neutral
}


public class EventHandler : MonoBehaviour
{

    List<Event> Events = new List<Event>();

    // Use this for initialization, called on script enabling
    void Start()
    {
        try
        {
            var serializer = new XmlSerializer(typeof(List<Event>));

            string xml = File.ReadAllText("Assets/TextResources/Events.xml");
            xml = XDocument.Parse(xml).ToString(SaveOptions.DisableFormatting);

            using (var stringReader = new StringReader(xml))
            {
                using (var reader = XmlReader.Create(stringReader))
                {
                    var result = (List<Event>)serializer.Deserialize(reader);
                    Events = result;
                }
            }
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
        }
    }
}

基本序列化方法

private void serializeList()
{
    XmlSerializer ser = new XmlSerializer(typeof(List<Event>));
    List<Event> list = new List<Event>();
    list.Add(new Event { EventId = 1, Description = "abc", EventType = EventType.Good, Cost = new Resources { Food = 0, Energy = 1, Happiness = 2, ShipHp = 3, Garbage = 4 }, Reward = new Resources { Food = 0, Energy = 1, Happiness = 2, ShipHp = 3, Garbage = 4 } });
    list.Add(new Event { EventId = 2, Description = "def", EventType = EventType.Good, Cost = new Resources { Food = 0, Energy = 1, Happiness = 2, ShipHp = 3, Garbage = 4 }, Reward = new Resources { Food = 0, Energy = 1, Happiness = 2, ShipHp = 3, Garbage = 4 } });
    list.Add(new Event { EventId = 3, Description = "ghi", EventType = EventType.Good, Cost = new Resources { Food = 0, Energy = 1, Happiness = 2, ShipHp = 3, Garbage = 4 }, Reward = new Resources { Food = 0, Energy = 1, Happiness = 2, ShipHp = 3, Garbage = 4 } });
    StreamWriter sw = new StreamWriter("Assets/TextResources/test.xml");
    ser.Serialize(sw, list);
    sw.Close();
}

答案 1 :(得分:0)

您的XML无效。您的第一个Resources标记永远不会关闭。