XML:写入文件是空的?

时间:2021-04-30 15:38:33

I've debugged it so far, the list has the values.. however its not writing them out to the file.

到目前为止我调试了它,列表中有值..但是它没有将它们写入文件。

I'm not sure why it's null.

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

The GameObjects class holds all the fields.

GameObjects类包含所有字段。

GameObjectData is just for the list.

GameObjectData仅用于列表。

Then ChestPlate inherits from GameObjects. The reason to this is, I'm making a game and all the values in GameObjects are relevant to ChestPlate.

然后ChestPlate继承自GameObjects。这样做的原因是,我正在制作游戏,GameObjects中的所有值都与ChestPlate相关。

Code is below:

代码如下:

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

1 个解决方案

#1


1  

I have run your code and it seems to serialize your data correctly the test file is created with the default parameters set by the base class.

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

What worries me a bit is the implementation I am sure you have more work to do but you need to dispose of objects correctly, ensure the attribute and serialization of the inherited classes and it parents are correctly done otherwise you will not be able to de-serialize correctly etc. I would suggest you look online for best practices for xml serialization in .net there are countless examples online but please see a few listed below.

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

Something else might be good to check is if you really need to use XML is json good enough or even binary serialization.

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

There is several community wiki entries on * and including questions about best practices have a hunt about.

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

Please below the output of the code you put together with the full source code below. Please note that i changed the location that you are saving the xml file to ".\test.xml" usually the bin folder and debug folder the app wont have any problems writing too like permissions etc.

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

Also note that on the xml below you have lost the fact that you serialized a ChestPlate object.

另请注意,在下面的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>

Code base used to generate the XML

用于生成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);
        }
    }
}

#1


1  

I have run your code and it seems to serialize your data correctly the test file is created with the default parameters set by the base class.

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

What worries me a bit is the implementation I am sure you have more work to do but you need to dispose of objects correctly, ensure the attribute and serialization of the inherited classes and it parents are correctly done otherwise you will not be able to de-serialize correctly etc. I would suggest you look online for best practices for xml serialization in .net there are countless examples online but please see a few listed below.

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

Something else might be good to check is if you really need to use XML is json good enough or even binary serialization.

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

There is several community wiki entries on * and including questions about best practices have a hunt about.

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

Please below the output of the code you put together with the full source code below. Please note that i changed the location that you are saving the xml file to ".\test.xml" usually the bin folder and debug folder the app wont have any problems writing too like permissions etc.

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

Also note that on the xml below you have lost the fact that you serialized a ChestPlate object.

另请注意,在下面的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>

Code base used to generate the XML

用于生成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);
        }
    }
}