在C#中序列化arraylist

phr*_*tic 3 c#

我有一个包含许多标准字段和一个arraylist的类.

有没有办法使用XmlSerializer序列化类?

到目前为止尝试导致错误消息说:

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.

类的一些简要表示如下所示:

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; }
  }
}
Run Code Online (Sandbox Code Playgroud)

toa*_*oad 5

我在以下方面取得了成功:

[XmlArray("HasTypeSpecialisations")]
[XmlArrayItem("TypeObject", typeof(TypeObject), IsNullable = false)]
public List<TypeObject> TypeSpecialisations
Run Code Online (Sandbox Code Playgroud)

这导致:

<HasTypeSpecialisations>
    <TypeObject />
    <TypeObject />
</HasTypeSpecialisations>
Run Code Online (Sandbox Code Playgroud)

在你的情况下,我会尝试这样的事情:

[XmlArrayItem(typeof(DataPoints))]
public ArrayList PolledData
Run Code Online (Sandbox Code Playgroud)

基于此链接http://msdn.microsoft.com/en-us/library/2baksw0z(VS.85).aspx你也应该能够使用这个

[XmlElement(Type = typeof(DataPoints))]
public ArrayList PolledData
Run Code Online (Sandbox Code Playgroud)