我想序列化一个类构造,它包含多达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>类构造如下:
[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“集合是只读的”。
我对硬定义的传感器也做过同样的尝试,序列化和反序列化没有问题:
public class MostModule
{
public SimpleSensor Sensor1 { get; set; }
public SimpleSensor Sensor2 { get; set; }是否有一种以这种方式序列化模块的方法,还是必须用硬定义的变量对其进行编程,这些变量必须从主目录中放入列表中?
或者有没有其他的建议来保存和加载我的数据在这种结构中?
发布于 2015-05-11 12:06:03
我创建了以下List<>对象
[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; }
}发布于 2015-05-11 11:02:17
将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;
}
}发布于 2015-05-11 11:23:20
尝尝这个
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; }
}
}https://stackoverflow.com/questions/30165288
复制相似问题