在 C# 中序列化一个数组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/664267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serializing an arraylist in C#
提问by phrenetic
I have a class that contains a number of standard fields and an arraylist.
我有一个包含许多标准字段和一个数组列表的类。
Is there any way to serialize the class using an XmlSerializer?
有没有办法使用 XmlSerializer 序列化类?
Attempts so far result in an error message saying:
到目前为止的尝试导致错误消息说:
Unhandled Exception: System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type XMLSerialization.DataPoints was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
Some cut-down representations of the classes are shown below:
类的一些缩减表示如下所示:
public class StationData
{
private DateTime _CollectionDate;
private string _StationID;
private ArrayList _PolledData;
public StationData()
{
}
public DateTime CollectionDate
{
get { return _CollectionDate; }
set { _CollectionDate = value; }
}
public string StationID
{
get { return _StationID; }
set { _StationID = value; }
}
[XmlInclude(typeof(DataPoints))]
public ArrayList PolledData
{
get { return _PolledData; }
set { _PolledData = value; }
}
}
public class DataPoints
{
private string _SubStationID;
private int _PointValue;
public DataPoints
{
}
public string SubStationID
{
get { return _SubStationID; }
set { _SubStationID = value; }
}
public int PointValue
{
get { return _PointValue; }
set { _PointValue = value; }
}
}
采纳答案by toad
I have had success with the following:
我在以下方面取得了成功:
[XmlArray("HasTypeSpecialisations")]
[XmlArrayItem("TypeObject", typeof(TypeObject), IsNullable = false)]
public List<TypeObject> TypeSpecialisations
This results in:
这导致:
<HasTypeSpecialisations>
<TypeObject />
<TypeObject />
</HasTypeSpecialisations>
In your situation I would try something like:
在您的情况下,我会尝试以下操作:
[XmlArrayItem(typeof(DataPoints))]
public ArrayList PolledData
Based on this link http://msdn.microsoft.com/en-us/library/2baksw0z(VS.85).aspxyou should also be able to use this
基于此链接http://msdn.microsoft.com/en-us/library/2baksw0z(VS.85).aspx你也应该能够使用这个
[XmlElement(Type = typeof(DataPoints))]
public ArrayList PolledData
回答by GvS
The XmlSerializer generates some code at runtime to serialize your class. It is necessary for this class to know all types that can occur.
XmlSerializer 在运行时生成一些代码来序列化您的类。这个类有必要知道所有可能出现的类型。
The ArrayList does not give this information, but you can give it by using a XmlIncludeattribute on the property that returns the ArrayList.
ArrayList 不提供此信息,但您可以通过在返回 ArrayList 的属性上使用XmlInclude属性来提供此信息。
[XmlInclude(typeof(DataPoints))]
public ArrayList Points {
...
}
You could also use the generic List<> class.
您还可以使用通用 List<> 类。
回答by Quintin Robinson
Take a look at this articlewhich describes the basic problem you are having and a solution around it (Like using the XmlInclude attribute). Basically what that says is that the serializer encountered a type it doesn't know how to serialize. If you post some code it would also help greatly.
看看这篇文章,它描述了您遇到的基本问题及其解决方案(例如使用 XmlInclude 属性)。基本上说的是序列化程序遇到了一个它不知道如何序列化的类型。如果您发布一些代码,它也会有很大帮助。
回答by Navaar
I think you could get around this by using a generic list (List<>) instead of an ArrayList, however, I'm going to assume you can't use generics for one reason or another.
我认为您可以通过使用泛型列表 (List<>) 而不是 ArrayList 来解决这个问题,但是,我假设您不能出于某种原因使用泛型。
You need to tell the compiler what type is contained in the ArrayList so it can serialize it since all it knows it that it contains objects.
您需要告诉编译器 ArrayList 中包含什么类型,以便它可以序列化它,因为它知道它包含对象。
Add this above your property and it should clear it up. [System.Xml.Serialization.XmlInclude(typeof(XMLSerialization.DataPoints))]
将此添加到您的财产上方,它应该会清除它。[System.Xml.Serialization.XmlInclude(typeof(XMLSerialization.DataPoints))]
Of course, replace XMLSerialization.DataPoints with whatever class is contained in the ArrayList.
当然,用 ArrayList 中包含的任何类替换 XMLSerialization.DataPoints。
回答by tsimon
The method I always used to serialize lists was to convert them to Arrays (which has the necessary type information). I admit this is a bit dirty, but if you can't get a proper list to serialize, this will work.
我一直用来序列化列表的方法是将它们转换为数组(具有必要的类型信息)。我承认这有点脏,但是如果您无法获得正确的序列化列表,这将起作用。