不同对象的序列化列表 [英] Serialization list of different object
本文介绍了不同对象的序列化列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我需要帮助,我不知道如何序列化/反序列化,我尝试了很多我在堆栈溢出时看到的东西,但是没有任何效果. 情况就在这里,我有一个创建列表的动物园课程
Hi everyone I need help I can't figure how to serialize /deserialize, and I tried lots of things that i saw on stack overflow and nothing worked. The situation is here, I have a zoo class that create a list
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-------------------");
}
}
}
列表中有像这样的不同动物
And the list is taking different object animals like this one
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;
}
}
此外,我使用这样的主函数,而保存和加载功能正是我要实现的目标
Moreover I use a main like this and the save and load function is what i'm trying to achieve
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();
}
}
对不起,因为我可能要问很多,但我只是不知道该怎么做.
Sorry because I'm maybe asking for a lot but i just don't know how to do it.
推荐答案
尝试以下代码:
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;
}
}
}
这篇关于不同对象的序列化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文