将ArrayList序列化/反序列化为XML文件 [英] Serializing/Deserializing an ArrayList to be an XML file
问题描述
我正在用C#编写Windows窗体应用程序.
有一个ArrayList包含多个对象.而且我成功编写了代码,将该ArrayList序列化为XML文件,并且可以反序列化此XML回到ArrayList.
但是,以上过程基于这样的假设:在进行序列化或反序列化时,我知道对象的长度(个数).
现在的问题是:
在我第一次反序列化它之前,如何知道退出的XML文件(包括序列化的ArrayList)中对象的长度(个数)?由于下面的指令需要一个DataType来匹配对象的长度?
Hi, I am writing a windows form application with C#.
There is one ArrayList which includes several objects. And I successfully wrote code to serialize this ArrayList to be an XML file, and can deserialize this XML to back to ArrayList.
However, the above process is based on the assumption that I know the length(number) of objects when doing serialization or deserialization.
The question and problem now is :
How can I know the length(number) of objects in an exited XML file (including serialized ArrayList) before I would first deserialize it ? Since the instruction below need a DataType to match the length of objects ?
Type[ ] DataType = new Type[ ? ] // how can I know the lenght ?
XmlSerializer xs_load = new XmlSerializer(typeof(ArrayList), DataType);
推荐答案
喜欢这个
Like this
XmlSerializer xs_load = new XmlSerializer(typeof(ArrayList), new Type[] {typeof(YourType1), typeof(YourType2), etc..});
干杯
Cheers
这篇关于将ArrayList序列化/反序列化为XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!