在C#中序列化arraylist [英] Serializing an arraylist in C#

查看:57
本文介绍了在C#中序列化arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多标准字段和一个数组列表的类.

I have a class that contains a number of standard fields and an arraylist.

是否可以使用XmlSerializer序列化类?

Is there any way to serialize the class using an 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; }
  }
}

推荐答案

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

I have had success with the following:

[XmlArray("HasTypeSpecialisations")]
[XmlArrayItem("TypeObject", typeof(TypeObject), IsNullable = false)]
public List<TypeObject> TypeSpecialisations

结果是:

<HasTypeSpecialisations>
    <TypeObject />
    <TypeObject />
</HasTypeSpecialisations>

在您的情况下,我会尝试以下操作:

In your situation I would try something like:

[XmlArrayItem(typeof(DataPoints))]
public ArrayList PolledData

基于此链接 http://msdn.microsoft.com/en-us/library/2baksw0z(VS.85).aspx ,您也应该可以使用它

Based on this link http://msdn.microsoft.com/en-us/library/2baksw0z(VS.85).aspx you should also be able to use this

[XmlElement(Type = typeof(DataPoints))]
public ArrayList PolledData

这篇关于在C#中序列化arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆