【问题标题】:Serialization list of different object不同对象的序列化列表
【发布时间】:2019-04-07 11:11:21
【问题描述】:

大家好,我需要帮助我不知道如何序列化/反序列化,我尝试了很多在堆栈溢出时看到的东西,但没有任何效果。 情况就在这里,我有一个创建列表的动物园类

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

namespace TP3
{
class Zoo
{
    private List<object> zooList;



    public Zoo()
    {
        zooList = new List<object>();
    }




    public void add ( object animal)
    {
        zooList.Add(animal);

    }

    public void remove(object animal)
    {
        zooList.Remove(animal);

    }

    public void clear( )
    {
        zooList.Clear();

    }


    public void move()
    {
        foreach(IAnimal specimen in zooList)
        {
             if (specimen is IMammal)
            {
                Console.Write("Mammal " + specimen);
                specimen.move();
            }

            else
            {
                Console.Write("Reptile "  + specimen );
                specimen.move();

            }
        }
    }

    public void eat()
    {
        foreach (IAnimal specimen in zooList)
        {
            if (specimen is IMammal)
            {
                Console.Write("Mammal " + specimen);
                specimen.eat();
            }

            else
            {
                Console.Write("Reptile " + specimen);
                specimen.eat();
            }
        }
    }


    public void count()
    {
        int compteur = 0;
        int countReptile= 0;
        int countMammal = 0;

        foreach (IAnimal specimen in zooList)
        {
            if (specimen is IMammal)
            {
                countMammal++;
            }

            else
            {
               countReptile++;
            }
            compteur++;
        }

        Console.WriteLine("Le zoo à "+ compteur + " animales");
        Console.WriteLine(countMammal + " mammifères, et " + countReptile+ " Reptiles");
    }

    public void display ()
    {

        Console.WriteLine("Le zoo contient :");
        Console.WriteLine("--------DEBUT-------------------");

        foreach (IAnimal specimen in zooList)
        {
            if (specimen is IMammal)
            {
                Console.WriteLine("Mammal " + specimen);
            }

            else
            {
                Console.WriteLine("Reptile "  + specimen);
            }
        }

        Console.WriteLine("--------FIN-------------------");

    }




    }

}

这个列表包含了不同的对象动物,比如这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TP3
{
 public class Lion : IMammal
{

    private string Name;

    public string _name
    {
        get { return Name; }
    }
    public Lion(string name)
    {
        Name = name;
    }



    public void move()
    {
        Console.WriteLine(" Superbe course du lion");
    }

    public void eat()
    {
        Console.WriteLine(" Bonne viande ");
    }

    public void NiceFur()
    {
        Console.WriteLine("Une crinière Majestueuse");

    }

    void IMammal.Lay()
    {

    }

    public override string ToString()
    {
        return _name;
    }

}

此外,我使用这样的主程序,而保存和加载功能正是我想要实现的目标

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TP3
{
class Program
{
    static void Main(string[] args)
    {
        Zoo vincennes = new Zoo();
        Cow cow1 = new Cow ("cow 1");
        Cow cow2 =  new  Cow  ( "cow 2" );
        // string file = @"C:\temp\zoosave.out";
        string file = Environment.CurrentDirectory + "\\myText.txt" ; 

        vincennes.add(new Lion ("lion 1"));
        vincennes.add(cow1);
        vincennes.add(new Lizard ("lizard 1"));
        vincennes.add(new Platypus("platypus 1"));
        vincennes.add(new Snake("snake 1"));
        vincennes.display(); 
        vincennes.remove(cow1);
        vincennes.remove(cow2);
        vincennes.display();
        vincennes.move();
        vincennes.eat();
        vincennes.count();

         vincennes.save(file);
        vincennes.clear();
        vincennes.display();

        vincennes.load(file);
        vincennes.display();

        System.Console.Read();
    }
}

对不起,我可能要求很多,但我不知道该怎么做。

【问题讨论】:

    标签: c# list object serialization deserialization


    【解决方案1】:

    您可以将数据存储到 JSON 文件中。我建议您使用适用于 .Net 的 Newtonsoft JSON 框架。

    https://www.nuget.org/packages/newtonsoft.json/

    在您可以看到的文档中,如何序列化和反序列化 JSON 文件:

    https://www.newtonsoft.com/json/help/html/SerializingJSON.htm

    【讨论】:

    • 我去看看
    【解决方案2】:

    试试下面的代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace TP3
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                Zoo vincennes = new Zoo();
                Cow cow1 = new Cow("cow 1");
                Cow cow2 = new Cow("cow 2");
    
                vincennes.add(new Lion("lion 1"));
                vincennes.add(cow1);
                vincennes.add(new Lizard("lizard 1"));
                vincennes.add(new Platypus("platypus 1"));
                vincennes.add(new Snake("snake 1"));
    
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                XmlWriter writer = XmlWriter.Create(FILENAME,settings);
                XmlSerializer serializer = new XmlSerializer(typeof(Zoo));
                serializer.Serialize(writer, vincennes);
    
                System.Console.Read();
            }
        }
        public class Zoo
        {
            private List<IAnimal> animals = new List<IAnimal>();
    
            public void add(IAnimal animal)
            {
                animals.Add(animal);
            }
    
            public List<IAnimal> _animals
            {
                get { return animals; }
            }
    
        }
        [XmlInclude(typeof(IMammal))]
        [XmlInclude(typeof(IReptile))]
        public class IAnimal
        {
            protected string Name;
            public IAnimal()
            {
            }
            public string _name
            {
                get { return Name; }
            }
        }
        [XmlInclude(typeof(Cow))]
        [XmlInclude(typeof(Lion))]
        [XmlInclude(typeof(Platypus))]
        public class IMammal : IAnimal
        {
        }
        [XmlInclude(typeof(Snake))]
        [XmlInclude(typeof(Lizard))]
        public class IReptile : IAnimal
        {
        }
        public class Lizard : IReptile
        {
            public Lizard() { }
            public Lizard(string name)
            {
                Name = name;
            }
        }
        public class Snake : IReptile
        {
            public Snake() { }
            public Snake(string name)
            {
                Name = name;
            }
        }
        public class Cow : IMammal
        {
            public Cow() { }
            public Cow(string name)
            {
                Name = name;
            }
        }
        public class Lion : IMammal
        {
            public Lion(){}
            public Lion(string name)
            {
                Name = name;
            }
        }
        public class Platypus : IMammal
        {
            public Platypus() { }
            public Platypus(string name)
            {
                Name = name;
            }
        }
    }
    

    【讨论】:

    • 嘿,谢谢,但我有几个问题。 -我的显示功能打印名称,如“cow 1”“cow 2”等......在我看到你创建的文件之后,如果我反序列化它还会打印“cow 1”等......? -对不起,我应该精确 IAnimal,IMammal 是接口(可能没用,但它是家庭作业)我应该以同样的方式处理 Xml 包含吗?感谢您的帮助
    • Xml Includes 是必需的,如果你把它们拿出来你会得到一个错误。反序列化后名称将保持不变。
    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    相关资源
    最近更新 更多