Why does serialization of double [] not work in WinCE?

I have an object with (de-) serializing its configuration through system.xml.serializer

The configuration in the class is as follows:

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 very well, but on Windows CE the Min / Max-Array just crashes after deactivation and then it is reserialized ("CheckThis" was put there as a test and follows after serializing the "Strom" values). Since KorrekturWerte is again a structure, depth cannot be a problem. [XmlArray ...] was not 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.

  • Perhaps this is not clear: I have serialized a class that contains all the values. Destroy it to initialize the class, and then repeat it as a debug check. Now the fields are missing. (The original file was serialized in XP, where it works correctly)

  • Changing a double [] to a list does not help. (Same result)

  • xml files: Original:

    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 true 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

Re-initialization (sorry CE is serialized on one 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 putting all the slices in slices. 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>(); } 

Debugging is not called, so no errors occur.

  • Version .net 2.0.
+4
source share
2 answers

Your min / max array must be initialized with new double[] or its null value, and you have nullREF exceptions and missing fields. Zero values ​​are not serialized or missing.

Edit2:

There seems to be a problem of deserializing arrays / lists for you. Please make the tag names of the array elements more explicit:

  [XmlArray("Min")] [XmlArrayItem("Value")] public double[] Min; [XmlArray("Max")] [XmlArrayItem("Value")] public double[] Max; 

and try if this helps you.

Edit3

From what you described in our discussion and chat, you ran into a real bug in the .NET Compact Framework 2.0.

Thus, it is best to use a custom Deserializer under CE if you cannot upgrade the Framework.

There were also some other errors described in CE here .

+5
source

Searching for other (working) solutions, I finally discovered the difference between them and my approach. I had an open double [], and then in some tests a public list. All other solutions had a private List <>, and then a public getter. (This is enough for List <> to serialize). Thus, changing its structure, everything works fine:

 public class Phase { public Int16 Trafo = 50; public KorrekturWerte Spannung = new KorrekturWerte() { Offset = 0, Steigung = 1 }; public KorrekturWerte Strom = new KorrekturWerte() { Offset = 0, Steigung = 1 }; private List<double> m_Min = new List<double>(); private List<double> m_Max = new List<double>(); public List<double> Min { get { return m_Min; } } public List<double> Max { get { return m_Max; } } //public double[] Default; } 
+3
source

Source: https://habr.com/ru/post/1398004/


All Articles