为什么序列化 double[] 在 WinCE 中不起作用?
我有一个对象,通过 system.xml.serializer 对其配置进行(反)序列化
该配置位于一个类中,如下所示:
public struct Phase
{
public Int16 Trafo;
public KorrekturWerte Spannung;
public KorrekturWerte Strom;
[XmlArray("Min")]
public double[] Min;
[XmlArray("Max")]
public double[] Max;
public bool CheckThis;
}
public class ParameterHardware
{
public string WAGOId = "00:30:DE:05:33:CB";
public Byte Phasen = 0x07;
public Phase L1;
public Phase L2;
public Phase L3;
}
在 WindowsXP 系统上(反)序列化它效果很好,但在 Windows CE 上,最小/Max-Array 只是在反序列化之后混乱(“CheckThis”被放在那里作为测试,并在序列化“Strom”值之后进行)。 由于 KorrekturWerte 又是一个结构,因此深度不是问题。 [XmlArray ...] 在我的第一个版本中不存在,它只是来自另一个测试。
编辑:
问题不仅仅在于序列化。尝试访问 Min[...] 我收到空引用错误。
也许还不清楚:我有一个类的序列化,其中包含所有值。反序列化它以初始化类,然后将其重新序列化作为调试检查。现在字段丢失了。 (原始文件在 XP 中序列化,工作正常)
将 double[] 更改为 List 没有帮助。 (相同的结果)
xml 文件: 原文:
00:30:DE:05:53:65 1 50 -0.2 1 0.004 0.994 0 0 0 0 0 500 32 15000 15000 1 真的 50 0 1 0 1 0 0 0 0 0 500 32 15000 15000 1 50 0 1 0 1 0 0 0 0 0 500 32 15000 15000 1
重新序列化(抱歉,CE 在一行中序列化):
<?xml version="1.0" encoding="utf-8"?><ClassTest_FCT_Extern xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Hardware><WAGOId>00:30:DE:05:53:65</WAGOId><Phasen>1</Phasen><L1><Trafo>50</Trafo><Spannung><Offset>-0.2</Offset><Steigung>1</Steigung></Spannung><Strom><Offset>0.004</Offset><Steigung>0.994</Steigung></Strom><CheckThis>true</CheckThis></L1><L2><Trafo>50</Trafo><Spannung><Offset>0</Offset><Steigung>1</Steigung></Spannung><Strom><Offset>0</Offset><Steigung>1</Steigung></Strom><CheckThis>false</CheckThis></L2><L3><Trafo>50</Trafo><Spannung><Offset>0</Offset><Steigung>1</Steigung></Spannung><Strom><Offset>0</Offset><Steigung>1</Steigung></Strom><CheckThis>false</CheckThis></L3></ClassTest_FCT_Extern>
抱歉将所有内容逐片进行。这是序列化代码(使用System.Xml.Serialization;)
<前><代码>尝试 { fstream = new FileStream(文件路径, FileMode.Open, FileAccess.Read); reader = new XmlTextReader(fstream); 序列化器 = new XmlSerializer(typeof(T)); retobj = (T)serializer.Deserialize(reader); } 捕获(异常 e) { Debug("序列化:"+e.ToString()); retobj = Activator.CreateInstance(); }
未调用调试,因此似乎没有任何错误。
- .net版本是2.0
I have an Object with (de-)serializes its configuration via system.xml.serializer
The config is in a class looking like this:
public struct Phase
{
public Int16 Trafo;
public KorrekturWerte Spannung;
public KorrekturWerte Strom;
[XmlArray("Min")]
public double[] Min;
[XmlArray("Max")]
public double[] Max;
public bool CheckThis;
}
public class ParameterHardware
{
public string WAGOId = "00:30:DE:05:33:CB";
public Byte Phasen = 0x07;
public Phase L1;
public Phase L2;
public Phase L3;
}
(De-)Serializing this on a WindowsXP-System works just fine, but on Windows CE, the Min/Max-Array is just mussing after de- and then reserializing ("CheckThis" was put there as a test and follows after serializing the "Strom" values).
As KorrekturWerte is again a struct, depth can't be the problem. The [XmlArray ...] wasn't there in my first version, it's just from another test.
Edit:
The Problem is not (only) in serialization. Trying to access Min[...] I get a null reference error.
Maybe it's not clear: I have a serialization of the class, which contains all values. Deserialize it to initialize the class and then reserialize it as a debug-check. Now the fields are missing. (The original file was serialized in XP, where it works all right)
Changeing the double[] to List does not help. (Same result)
The xml-files:
Original:00:30:DE:05:53:65
150
-0.2
10.004
0.9940
0
0
0
0500
32
15000
15000
1true
50
0
10
10
0
0
0
0500
32
15000
15000
150
0
10
10
0
0
0
0500
32
15000
15000
1
Reserialization (sorry, CE serializes in one single line):
<?xml version="1.0" encoding="utf-8"?><ClassTest_FCT_Extern xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Hardware><WAGOId>00:30:DE:05:53:65</WAGOId><Phasen>1</Phasen><L1><Trafo>50</Trafo><Spannung><Offset>-0.2</Offset><Steigung>1</Steigung></Spannung><Strom><Offset>0.004</Offset><Steigung>0.994</Steigung></Strom><CheckThis>true</CheckThis></L1><L2><Trafo>50</Trafo><Spannung><Offset>0</Offset><Steigung>1</Steigung></Spannung><Strom><Offset>0</Offset><Steigung>1</Steigung></Strom><CheckThis>false</CheckThis></L2><L3><Trafo>50</Trafo><Spannung><Offset>0</Offset><Steigung>1</Steigung></Spannung><Strom><Offset>0</Offset><Steigung>1</Steigung></Strom><CheckThis>false</CheckThis></L3></ClassTest_FCT_Extern>
Sorry for bringing everything slice by slice. Here is the serialization code (using System.Xml.Serialization;)
try { fstream = new FileStream(filepath, FileMode.Open, FileAccess.Read); reader = new XmlTextReader(fstream); serializer = new XmlSerializer(typeof(T)); retobj = (T)serializer.Deserialize(reader); } catch (Exception e) { Debug("Serialization: "+e.ToString()); retobj = Activator.CreateInstance<T>(); }
Debug is not called, so there don't seem to be any errors.
- The .net Version is 2.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的最小/最大数组必须使用 new double[] 或其 null 进行初始化,并且您会遇到 nullref 异常和缺失字段。空值未序列化并且丢失。
编辑2:
似乎为您反序列化数组/列表时出现问题。请使数组项的标签名称更加明确,如下所示:
并尝试这是否对您有帮助。
Edit3
根据您在我们的讨论和聊天中所描述的内容,您一定在 .NET Compact Framework 2.0 中遇到了真正的错误。
因此,您最好的选择是使用 自定义反序列化器 在CE下,如果您无法更新框架。
CE 下还报告了一些其他错误 此处。
Your min/max array must be initialized with
new double[]
or its null and you have nullref exceptions and missing fields. Null values are not serialized and are missing.Edit2:
Seems like there is a problem deserializing arrays/lists for you. Please make the tag names of the array items more explicite like this:
and try if that helps you.
Edit3
From what you described in our discussion and chat you must have encountered a real bug in .NET Compact Framework 2.0.
So propably your best bet is to use a custom Deserializer under CE, if you can't update the Framework.
There were also some other bugs reported under CE here.
在寻找其他(有效的)解决方案时,我终于发现它们与我的方法之间存在差异。我有一个公共 double[],或者在某些测试中有一个公共列表。所有其他解决方案都有一个私有列表<>然后是公共吸气剂。 (这足以让 List<> 进行序列化)。相应地改变我的结构阶段,现在一切正常:
Searching for other (working) solutions, I finally discovered a difference between them and my approach. I had a public double[] or then in some tests a public List. All the other solutions had a privat List<> and then a public getter. (Which is enough for a List<> to serialize). Changeing my struct phase accordingly, everything works now fine: