我希望序列化一个类构造,其中包含最多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;
}
当我尝试序列化此类时,它可以正常工作,但反序列化会因错误和内部异常而停止: NotSupportedException&#34;该集合是只读的。&#34;
我尝试使用硬定义的传感器,序列化和反序列化没有问题:
public class MostModule
{
public SimpleSensor Sensor1 { get; set; }
public SimpleSensor Sensor2 { get; set; }
有没有办法以这种方式序列化模块,或者我是否必须使用硬件定义的变量对它进行编程,我必须将其放入主列表中?
或者是否有其他建议在这种结构中保存和加载我的数据?
答案 0 :(得分:0)
将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;
}
}
答案 1 :(得分:0)
试试这个
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; }
}
}
答案 2 :(得分:0)
我做了以下列表&lt;&gt;对象
[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; }
}