Deserialize a class with list of class containing a list: Error: "the collection is read-only"

Deserialize a class with list of class containing a list: Error: "the collection is read-only"

我想序列化一个 class 结构,其中包含最多 16 个传感器的列表(以及最多包含 4 个 PressureSensors 的列表),其中包含一个包含其函数系数的列表(以及一些更多信息,例如 ID ...)。

PressureSensor 是附加的,只是表明模块中还有一些集合。否则

List<List<SimpleSensor>> 

就足够了。

XML创建没有问题,只是反序列化失败

<?xml version="1.0" encoding="utf-8"?>
<Module xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SingleSensors>
    <SSensor>
      <ID>Sensor1</ID>
      <Function>
        <double>0</double>
        <double>1</double>
      </Function>
    </SSensor>
    <SSensor>
      <ID>Sensor2</ID>
      <Function>
        <double>0</double>
        <double>1</double>
      </Function>
    </SSensor>
  </SingleSensors>
  <PressureSensors>
    <PSensor>
      <ID>Pressure1</ID>
      <Function>
        <double>0</double>
        <double>1</double>
      </Function>
    </PSensor>
    <PSensor>
      <ID>Pressure2</ID>
      <Function>
        <double>0</double>
        <double>1</double>
      </Function>
    </PSensor>
  </PressureSensors>
</Module>

Class构造如下:

[Serializable]
public class SimpleSensor
{
     [XmlElement("ID")]
     public string Id { get; set; }

    [XmlArray("Function")]
    public Collection<double> Coefficient { get; set; }

    //## Constructor:
    public SimpleSensor()
    {
        Id = "00";
        double[] content = new double[2] { 0, 1 };
        Coefficient = new  Collection<double>(content);           
    }
}

[Serializable]
[XmlRoot("Module")]
public class MostModule
{
    [XmlArray("SingleSensors")]
    [XmlArrayItem("SSensor")]
    public List<SimpleSensor> Sensor { get; set; }

    [XmlArray("PressureSensors")]
    [XmlArrayItem("PSensor")]
    public List<SimpleSensor> PressureSensor { get; set; }



    //## Constructor:
    public MostModule()
    {            
        //Initialise SimpleSensors with 2 Sensors
        SimpleSensor[] SensorArray = new SimpleSensor[2];                        
        for (int i = 0; i < 2; i++)
        {
            SensorArray[i] = new SimpleSensor();
            SensorArray[i].Id = "Sensor" + (i + 1);
        }
        Sensor = new List<SimpleSensor>(SensorArray);
        PressureSensor = new List<SimpleSensor>(SensorArray);
    }

    public static MostModule Deserialize(string fileName)
    {
        MostModule Sensors;
        XmlSerializer myXMLSerial = new XmlSerializer(typeof(MostModule));
        using (StreamReader sr = File.OpenText(fileName))
        {
            Sensors = (MostModule)myXMLSerial.Deserialize(sr);                   
        }
        return Sensors;           
    }

当我尝试序列化这个 class 它工作正常,但反序列化停止并出现错误和内部异常: NotSupportedException "The collection is read-only."

我对硬定义的传感器进行了同样的尝试,序列化和反序列化没有问题:

public class MostModule
{
    public SimpleSensor Sensor1 { get; set; }
    public SimpleSensor Sensor2 { get; set; }

有没有办法以这种方式序列化模块,或者我是否必须使用硬定义的变量对其进行编程,我必须将这些变量放入主列表中?

或者对于在这种构造中保存和加载我的数据还有其他建议吗?

SimpleSensor.Coefficient 定义为普通数组,它将起作用:

[Serializable]
public class SimpleSensor
{
    [XmlElement("ID")]
    public string Id { get; set; }

    [XmlArray("Function")]
    public double[] Coefficient { get; set; }

    //## Constructor:
    public SimpleSensor()
    {
        Id = "00";
        double[] content = new double[2] { 0, 1 };
        Coefficient = content;           
    }
}

试试这个

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication27
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlSerializer xs = new XmlSerializer(typeof(MostModule));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            MostModule MostModule = (MostModule)xs.Deserialize(reader);

        }
    }


    [XmlRoot("Function")]
    public class Function
    {
        [XmlElement("double")]
        public List<int> m_double {get;set;}
    }

    public class SSensor
    {
        [XmlElement("ID")]
        public string Id { get; set; }

        [XmlElement("Function")]
        public Function Coefficient { get; set; }

    }
    [XmlRoot("SingleSensors")]
    public class SimpleSensor
    {
        [XmlElement("SSensor")]
        public SSensor sSensor { get; set; }

    }

    [XmlRoot("PressureSensors")]
    public class PressureSensors
    {
        [XmlElement("PSensor")]
        public SSensor sSensor { get; set; }

    }

    [XmlRoot("Module")]
    public class MostModule
    {
        [XmlElement("SingleSensors")]
        public SimpleSensor Sensor { get; set; }

        [XmlElement("PressureSensors")]
        public PressureSensors PressureSensor { get; set; }

    }
}

我创建了以下列表<>对象

 [XmlRoot("SingleSensors")]
    public class SimpleSensor
    {
        [XmlElement("SSensor")]
        public List<SSensor> sSensor { get; set; }

    }

    [XmlRoot("PressureSensors")]
    public class PressureSensors
    {
        [XmlElement("PSensor")]
        public List<SSensor> sSensor { get; set; }

    }