-3

私はこれまでデバッグしましたが、リストには値がありますが、ファイルに書き出されていません。

なぜnullなのかわかりません。

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

私はあなたのコードを実行しましたが、データを正しくシリアル化しているようです。テスト ファイルは、基本クラスによって設定された既定のパラメーターで作成されます。

私が少し心配しているのは実装です。もっとやるべきことがあると思いますが、オブジェクトを正しく破棄し、継承されたクラスの属性とシリアル化を確実にし、その親が正しく行われるようにする必要があります。 .net での xml シリアライゼーションのベスト プラクティスについてオンラインで調べることをお勧めします。オンラインには無数の例がありますが、以下にリストされているいくつかを参照してください。

XMLを本当に使用する必要があるかどうかは、jsonで十分か、バイナリシリアル化でさえあるかどうかを確認するのに良いかもしれません。

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 に答える