首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用包含列表的类列表反序列化类: Error:“集合是只读的”

用包含列表的类列表反序列化类: Error:“集合是只读的”
EN

Stack Overflow用户
提问于 2015-05-11 10:26:26
回答 3查看 153关注 0票数 0

我想序列化一个类构造,它包含多达16个传感器的列表(附加一个包含最多4个PressureSensors的列表),其中包含一个带有函数系数的列表(以及ID .之类的更多信息)。

PressureSensor是额外的,它只是显示了模块中有更多的集合。否则

代码语言:javascript
复制
List<List<SimpleSensor>> 

就够了。

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

代码语言:javascript
复制
<?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>

类构造如下:

代码语言:javascript
复制
[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;           
    }

当我试图序列化这个类时,它正常工作,但是反序列化由于一个错误和内部异常而停止: NotSupportedException“集合是只读的”。

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

代码语言:javascript
复制
public class MostModule
{
    public SimpleSensor Sensor1 { get; set; }
    public SimpleSensor Sensor2 { get; set; }

是否有一种以这种方式序列化模块的方法,还是必须用硬定义的变量对其进行编程,这些变量必须从主目录中放入列表中?

或者有没有其他的建议来保存和加载我的数据在这种结构中?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-05-11 12:06:03

我创建了以下List<>对象

代码语言:javascript
复制
 [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; }

    }
票数 0
EN

Stack Overflow用户

发布于 2015-05-11 11:02:17

SimpleSensor.Coefficient定义为一个普通数组,它将工作:

代码语言:javascript
复制
[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;           
    }
}
票数 0
EN

Stack Overflow用户

发布于 2015-05-11 11:23:20

尝尝这个

代码语言:javascript
复制
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; }

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30165288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档