不同对象的序列化列表

时间:2018-11-03 13:02:28

标签: c# list object serialization deserialization

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

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

对不起,因为我可能要问很多,但我只是不知道该怎么做。

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

尝试以下代码:

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