Xml序列化序列问题

通常,这一切都非常适合将我的对象序列化为Xml字符串并再次返回到对象中。 但是,我在不同的最终用户之间存在这种不一致,我似乎无法追查。

基本上,当我序列化一个对象时,它看起来像转换为Xml后:

  Step 2 20   Motor 450 KW Pump  1  400 Volt 3 Phase 12w 50Hz  400 415 Wye 3 231/400v F true 50 0.8000 false false  F   true KWm 0  0.90 450  false NEMA VariableFrequencyDrive F 0 5.3   Motor 450 KW Pump  1  400 Volt 3 Phase 12w 50Hz  400 415 Wye 3 231/400v F true 50 0.8000 false false  F   true KWm 0  0.90 450  false NEMA VariableFrequencyDrive F 0 5.3   Motor 450 KW Pump  1  400 Volt 3 Phase 12w 50Hz  400 415 Wye 3 231/400v F true 50 0.8000 false false  F   true KWm 0  0.90 450  false NEMA VariableFrequencyDrive F 0 5.3    

但是,当其他随机用户运行我的程序并对序列化对象时,他们最终会改为:

   Step 2   Motor 450 KW Pump  1  240 Volt 3 Phase 12w 60Hz 120/240v 3 240 480 Delta 3 J true 60 0.8000 true true  B C D E   true KWm 0  0.90 450  false NEMA VariableFrequencyDrive F 0 5.3   Motor 450 KW Pump  1  240 Volt 3 Phase 12w 60Hz 120/240v 3 240 480 Delta 3 J true 60 0.8000 true true  B C D E   true KWm 0  0.90 450  false NEMA VariableFrequencyDrive F 0 5.3   Motor 450 KW Pump  1  240 Volt 3 Phase 12w 60Hz 120/240v 3 240 480 Delta 3 J true 60 0.8000 true true  B C D E   true KWm 0  0.90 450  false NEMA VariableFrequencyDrive F 0 5.3   20  

你会发现它的不同之处在于VoltageDipLoads的顺序不同。 所以它似乎与模式中设置的序列不匹配。 在用户反序列化Xml字符串之后会发生什么,Name,Loads和VoltageDip属性设置为默认值(引用类型和字符串为null,int为0)而不是序列化过程中保存的值。

使用该应用程序的每个人都使用相同的源代码。

谁能告诉我发生了什么? 为什么不同用户/机器之间的Serializtion元素顺序会发生变化? 为什么反序列化过程在发生时不能处理?

尝试在属性的XmlElement属性上使用order for ex –

 [XmlElement(Order = 1)] public string Prop1{get;set;} [XmlElement(Order = 2)] public string Prop1{get;set;} 

装饰你的xml类,特别是order参数(另见: Other StackOverFlow问题 )。

 using System; using System.Collections.Specialized; using System.IO; using System.Reflection; using System.Xml.Schema; using System.Xml.Serialization; using PSSpecClassLibrary.Attributes; using PSSpecClassLibrary.Sizing.Loads; using PSSpecClassLibrary.Utilities; namespace PSSpecClassLibrary.Sizing { ///  /// Class to represent a step in a sizing project. ///  [Serializable] public class Step : PSSpecObject { #region Fields private int m_intVoltageDip; private StepList m_stepList; private LoadList m_loads; #endregion #region Properties ///  /// Returns an XmlSchemaSet for this object type. ///  [XmlIgnore] public static XmlSchemaSet XmlSchemaSet { get { try { XmlSchemaSet xmlSchemaSet = new XmlSchemaSet(); using (Stream stream = Assembly.GetAssembly(typeof(Step)).GetManifestResourceStream("PSSpecClassLibrary.Sizing.Step.xsd")) { xmlSchemaSet.Add(XmlSchema.Read(stream, XmlSchemaReadValidationCallBack)); } using (Stream stream = Assembly.GetAssembly(typeof(Step)).GetManifestResourceStream("PSSpecClassLibrary.Guid.xsd")) { xmlSchemaSet.Add(XmlSchema.Read(stream, XmlSchemaReadValidationCallBack)); } return xmlSchemaSet; } catch { return null; } } } ///  /// The Step Name ///  [XmlElement(Order = 1)] public override string Name { get { return base.Name; } set { base.Name = value; } } ///  /// Step Voltage Dip ///  [XmlElement(Order = 2)] public int VoltageDip { get { return m_intVoltageDip; } set { m_intVoltageDip = value; } } ///  /// Step Loads Collection ///  [XmlElement(Order = 3)] public LoadList Loads { get { return m_loads; } set { if (m_loads != value) { if (m_loads != null) m_loads.CollectionChanged -= LoadsChanged; if (value != null) { value.CollectionChanged -= LoadsChanged; value.CollectionChanged += LoadsChanged; } } m_loads = value; } } #endregion #region Methods ///  /// Creates an instance of the class. ///  public Step() { m_loads = new LoadList(this); } #endregion } }