-3

到目前为止,我已经对其进行了调试,该列表具有值..但是它没有将它们写出到文件中。

我不确定为什么它是空的。

该类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();
     }
 }
4

1 回答 1

1

我已经运行了你的代码,它似乎正确地序列化了你的数据,测试文件是使用基类设置的默认参数创建的。

让我有点担心的是实现我相信你还有更多工作要做,但你需要正确处理对象,确保继承类的属性和序列化以及它的父类正确完成,否则你将无法 de-正确序列化等。我建议您在网上查找 .net 中 xml 序列化的最佳实践,网上有无数示例,但请参阅下面列出的一些示例。

其他可能需要检查的是,如果您真的需要使用 XML 是否足够好,甚至是二进制序列化。

stackoverflow 上有几个社区 wiki 条目,其中包括有关最佳实践的问题。

请在下面的代码输出下方加上完整的源代码。请注意,我将您保存 xml 文件的位置更改为“.\test.xml”,通常是 bin 文件夹和调试文件夹,该应用程序在写入时不会遇到任何问题,例如权限等。

另请注意,在下面的 xml 中,您已经丢失了序列化 ChestPlate 对象的事实。

<?xml version="1.0" encoding="utf-8"?>
<GameObjectData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <GameList>
    <GameObjects>
      <Item_Name>OMG</Item_Name>
      <Item_type>CHESTPLATE</Item_type>
      <Item_Level>5</Item_Level>
      <Item_description>omg</Item_description>
      <Item_ID>1234</Item_ID>
    </GameObjects>
  </GameList>
</GameObjectData>

用于生成 XML 的代码库

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ConsoleApplication12
{
    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();
        }

    }

    [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;
        }

        public GameObjects()
        {

        }

    }

    [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 ChestPlate()
        {


        }


        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(@".\Test.xml");

            serializer.Serialize(textWriter, f);

            Console.WriteLine(f);
        }
    }
}
于 2013-03-08T18:13:27.057 回答