序列化对象的序列化XML泛型列表
我可以序列化序列化对象的一个泛型列表,而不必指定其类型。
Can I serialize a generic list of serializable objects without having to specify their type.
类似下面的破code背后的意图:
Something like the intention behind the broken code below:
List<ISerializable> serializableList = new List<ISerializable>();
XmlSerializer xmlSerializer = new XmlSerializer(serializableList.GetType());
serializableList.Add((ISerializable)PersonList);
using (StreamWriter streamWriter = System.IO.File.CreateText(fileName))
{
xmlSerializer.Serialize(streamWriter, serializableList);
}
编辑:
对于那些谁想要知道的细节:当我尝试运行此code,它在错误的XMLSerializer [...]本着:
For those who wanted to know detail: when I try to run this code, it errors on the XMLSerializer[...] line with:
无法序列接口System.Runtime.Serialization.ISerializable。
Cannot serialize interface System.Runtime.Serialization.ISerializable.
如果更改为列表&LT;对象&gt;
我得到。有一个错误生成XML文档
。设置InnerException细节是{类型System.Collections.Generic.List1 [Project1.Person,ConsoleFramework,版本= 1.0.0.0,文化=中立,公钥=空]可能不会在使用这样的背景下。}
If I change to List<object>
I get "There was an error generating the XML document."
. The InnerException detail is "{"The type System.Collections.Generic.List1[[Project1.Person, ConsoleFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] may not be used in this context."}"
的人对象的定义如下:
[XmlRoot("Person")]
public class Person
{
string _firstName = String.Empty;
string _lastName = String.Empty;
private Person()
{
}
public Person(string lastName, string firstName)
{
_lastName = lastName;
_firstName = firstName;
}
[XmlAttribute(DataType = "string", AttributeName = "LastName")]
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
[XmlAttribute(DataType = "string", AttributeName = "FirstName")]
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
}
该PersonList只是一个列表与LT;人方式&gt;
这是只是为了测试虽然,所以没感觉到细节太重要了。关键是我有一个或多个不同的对象,所有这些都是可序列化。我想他们都序列化到一个文件。我认为这样做将是把它们放在一个通用的列表,并一气呵成序列列表的最简单方法。但是,这是行不通的。
This is just for testing though, so didn't feel the details were too important. The key is I have one or more different objects, all of which are serializable. I want to serialize them all to one file. I thought the easiest way to do that would be to put them in a generic list and serialize the list in one go. But this doesn't work.
我列表与LT试图
为好,但失败
System.Xml.Serialization.IXmlSerializable不能被序列化,因为它没有一个参数的构造函数。
对不起缺乏细节,但我这个初学者,不知道需要什么样的细节。如果人们要求更多的细节尝试的方式,会离开我理解需要什么样的细节,或一个基本答案,概述可能的方向应对这将是有益的。
Sorry for the lack of detail, but I am a beginner at this and don't know what detail is required. It would be helpful if people asking for more detail tried to respond in a way that would leave me understanding what details are required, or a basic answer outlining possible directions.
还有感谢的两个答案到目前为止,我已经得到了 - 我可以花更多的时间读书没有得到这些想法。令人惊讶的是人们如何帮助的是关于这个网站。
Also thanks to the two answers I've got so far - I could have spent a lot more time reading without getting these ideas. It's amazing how helpful people are on this site.
我有一个泛型列表℃的解决方案;>动态绑定物品
I have an solution for a generic List<> with dynamic binded items.
类PersonalList它的根元素
class PersonalList it's the root element
[XmlRoot("PersonenListe")]
[XmlInclude(typeof(Person))] // include type class Person
public class PersonalList
{
[XmlArray("PersonenArray")]
[XmlArrayItem("PersonObjekt")]
public List<Person> Persons = new List<Person>();
[XmlElement("Listname")]
public string Listname { get; set; }
// Konstruktoren
public PersonalList() { }
public PersonalList(string name)
{
this.Listname = name;
}
public void AddPerson(Person person)
{
Persons.Add(person);
}
}
类Person它是一个单一的列表元素
class Person it's an single list element
[XmlType("Person")] // define Type
[XmlInclude(typeof(SpecialPerson)), XmlInclude(typeof(SuperPerson))]
// include type class SpecialPerson and class SuperPerson
public class Person
{
[XmlAttribute("PersID", DataType = "string")]
public string ID { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("City")]
public string City { get; set; }
[XmlElement("Age")]
public int Age { get; set; }
// Konstruktoren
public Person() { }
public Person(string name, string city, int age, string id)
{
this.Name = name;
this.City = city;
this.Age = age;
this.ID = id;
}
}
类SpecialPerson继承人
class SpecialPerson inherits Person
[XmlType("SpecialPerson")] // define Type
public class SpecialPerson : Person
{
[XmlElement("SpecialInterests")]
public string Interests { get; set; }
public SpecialPerson() { }
public SpecialPerson(string name, string city, int age, string id, string interests)
{
this.Name = name;
this.City = city;
this.Age = age;
this.ID = id;
this.Interests = interests;
}
}
类SuperPerson继承人
class SuperPerson inherits Person
[XmlType("SuperPerson")] // define Type
public class SuperPerson : Person
{
[XmlArray("Skills")]
[XmlArrayItem("Skill")]
public List<String> Skills { get; set; }
[XmlElement("Alias")]
public string Alias { get; set; }
public SuperPerson()
{
Skills = new List<String>();
}
public SuperPerson(string name, string city, int age, string id, string[] skills, string alias)
{
Skills = new List<String>();
this.Name = name;
this.City = city;
this.Age = age;
this.ID = id;
foreach (string item in skills)
{
this.Skills.Add(item);
}
this.Alias = alias;
}
}
和主要的测试源
static void Main(string[] args)
{
PersonalList personen = new PersonalList();
personen.Listname = "Friends";
// normal person
Person normPerson = new Person();
normPerson.ID = "0";
normPerson.Name = "Max Man";
normPerson.City = "Capitol City";
normPerson.Age = 33;
// special person
SpecialPerson specPerson = new SpecialPerson();
specPerson.ID = "1";
specPerson.Name = "Albert Einstein";
specPerson.City = "Ulm";
specPerson.Age = 36;
specPerson.Interests = "Physics";
// super person
SuperPerson supPerson = new SuperPerson();
supPerson.ID = "2";
supPerson.Name = "Superman";
supPerson.Alias = "Clark Kent";
supPerson.City = "Metropolis";
supPerson.Age = int.MaxValue;
supPerson.Skills.Add("fly");
supPerson.Skills.Add("strong");
// Add Persons
personen.AddPerson(normPerson);
personen.AddPerson(specPerson);
personen.AddPerson(supPerson);
// Serialize
Type[] personTypes = { typeof(Person), typeof(SpecialPerson), typeof(SuperPerson) };
XmlSerializer serializer = new XmlSerializer(typeof(PersonalList), personTypes);
FileStream fs = new FileStream("Personenliste.xml", FileMode.Create);
serializer.Serialize(fs, personen);
fs.Close();
personen = null;
// Deserialize
fs = new FileStream("Personenliste.xml", FileMode.Open);
personen = (PersonalList)serializer.Deserialize(fs);
serializer.Serialize(Console.Out, personen);
Console.ReadLine();
}
重要的是定义,包括有多种不同的类型。
Important is the definition and includes of the diffrent types.