通常所有这一切都适用于将我的对象序列化为Xml字符串并再次返回到对象中。但是,我在不同的最终用户之间存在这种不一致的情况,我似乎无法追查。
基本上,当我序列化一个对象时,它看起来像转换为Xml:
<?xml version="1.0" encoding="utf-8"?>
<Step xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Step 2</Name>
<VoltageDip>20</VoltageDip>
<Loads>
<Load xsi:type="ThreePhaseMotorLoad">
<Name>Motor 450 KW Pump</Name>
<Comments />
<Quantity>1</Quantity>
<Voltage>
<Name>400 Volt 3 Phase 12w 50Hz</Name>
<V3Id xsi:nil="true" />
<Value>400</Value>
<V3Value>415</V3Value>
<Connection>Wye</Connection>
<Phase>3</Phase>
<RangeDescription>231/400v</RangeDescription>
<VoltageCode>F</VoltageCode>
<Active>true</Active>
<Frequency>50</Frequency>
<PowerFactor>0.8000</PowerFactor>
<UL2200>false</UL2200>
<Default>false</Default>
<TempRiseIds>
<TempRiseId>F</TempRiseId>
</TempRiseIds>
</Voltage>
<IsNonLinear>true</IsNonLinear>
<PriorityProperty>KWm</PriorityProperty>
<KW>0</KW>
<RunningPowerFactor xsi:nil="true" />
<StartingPowerFactor>0.90</StartingPowerFactor>
<KWm>450</KWm>
<Efficiency xsi:nil="true" />
<IsLowInertia>false</IsLowInertia>
<MotorStandard>NEMA</MotorStandard>
<MotorStartingMethod>VariableFrequencyDrive</MotorStartingMethod>
<NEMAMotorCode>F</NEMAMotorCode>
<SolidStateStarterPercent>0</SolidStateStarterPercent>
<LockedRotorKVAPerHP>5.3</LockedRotorKVAPerHP>
</Load>
<Load xsi:type="ThreePhaseMotorLoad">
<Name>Motor 450 KW Pump</Name>
<Comments />
<Quantity>1</Quantity>
<Voltage>
<Name>400 Volt 3 Phase 12w 50Hz</Name>
<V3Id xsi:nil="true" />
<Value>400</Value>
<V3Value>415</V3Value>
<Connection>Wye</Connection>
<Phase>3</Phase>
<RangeDescription>231/400v</RangeDescription>
<VoltageCode>F</VoltageCode>
<Active>true</Active>
<Frequency>50</Frequency>
<PowerFactor>0.8000</PowerFactor>
<UL2200>false</UL2200>
<Default>false</Default>
<TempRiseIds>
<TempRiseId>F</TempRiseId>
</TempRiseIds>
</Voltage>
<IsNonLinear>true</IsNonLinear>
<PriorityProperty>KWm</PriorityProperty>
<KW>0</KW>
<RunningPowerFactor xsi:nil="true" />
<StartingPowerFactor>0.90</StartingPowerFactor>
<KWm>450</KWm>
<Efficiency xsi:nil="true" />
<IsLowInertia>false</IsLowInertia>
<MotorStandard>NEMA</MotorStandard>
<MotorStartingMethod>VariableFrequencyDrive</MotorStartingMethod>
<NEMAMotorCode>F</NEMAMotorCode>
<SolidStateStarterPercent>0</SolidStateStarterPercent>
<LockedRotorKVAPerHP>5.3</LockedRotorKVAPerHP>
</Load>
<Load xsi:type="ThreePhaseMotorLoad">
<Name>Motor 450 KW Pump</Name>
<Comments />
<Quantity>1</Quantity>
<Voltage>
<Name>400 Volt 3 Phase 12w 50Hz</Name>
<V3Id xsi:nil="true" />
<Value>400</Value>
<V3Value>415</V3Value>
<Connection>Wye</Connection>
<Phase>3</Phase>
<RangeDescription>231/400v</RangeDescription>
<VoltageCode>F</VoltageCode>
<Active>true</Active>
<Frequency>50</Frequency>
<PowerFactor>0.8000</PowerFactor>
<UL2200>false</UL2200>
<Default>false</Default>
<TempRiseIds>
<TempRiseId>F</TempRiseId>
</TempRiseIds>
</Voltage>
<IsNonLinear>true</IsNonLinear>
<PriorityProperty>KWm</PriorityProperty>
<KW>0</KW>
<RunningPowerFactor xsi:nil="true" />
<StartingPowerFactor>0.90</StartingPowerFactor>
<KWm>450</KWm>
<Efficiency xsi:nil="true" />
<IsLowInertia>false</IsLowInertia>
<MotorStandard>NEMA</MotorStandard>
<MotorStartingMethod>VariableFrequencyDrive</MotorStartingMethod>
<NEMAMotorCode>F</NEMAMotorCode>
<SolidStateStarterPercent>0</SolidStateStarterPercent>
<LockedRotorKVAPerHP>5.3</LockedRotorKVAPerHP>
</Load>
</Loads>
</Step>
然而,当其他随机用户运行我的程序并将序列化对象时,他们最终会改为:
<?xml version="1.0" encoding="utf-8"?>
<Step xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Step 2</Name>
<Loads>
<Load xsi:type="ThreePhaseMotorLoad">
<Name>Motor 450 KW Pump</Name>
<Comments />
<Quantity>1</Quantity>
<Voltage>
<Name>240 Volt 3 Phase 12w 60Hz</Name>
<RangeDescription>120/240v</RangeDescription>
<V3Id>3</V3Id>
<Value>240</Value>
<V3Value>480</V3Value>
<Connection>Delta</Connection>
<Phase>3</Phase>
<VoltageCode>J</VoltageCode>
<Active>true</Active>
<Frequency>60</Frequency>
<PowerFactor>0.8000</PowerFactor>
<UL2200>true</UL2200>
<Default>true</Default>
<TempRiseIds>
<TempRiseId>B</TempRiseId>
<TempRiseId>C</TempRiseId>
<TempRiseId>D</TempRiseId>
<TempRiseId>E</TempRiseId>
</TempRiseIds>
</Voltage>
<IsNonLinear>true</IsNonLinear>
<PriorityProperty>KWm</PriorityProperty>
<KW>0</KW>
<RunningPowerFactor xsi:nil="true" />
<StartingPowerFactor>0.90</StartingPowerFactor>
<KWm>450</KWm>
<Efficiency xsi:nil="true" />
<IsLowInertia>false</IsLowInertia>
<MotorStandard>NEMA</MotorStandard>
<MotorStartingMethod>VariableFrequencyDrive</MotorStartingMethod>
<NEMAMotorCode>F</NEMAMotorCode>
<SolidStateStarterPercent>0</SolidStateStarterPercent>
<LockedRotorKVAPerHP>5.3</LockedRotorKVAPerHP>
</Load>
<Load xsi:type="ThreePhaseMotorLoad">
<Name>Motor 450 KW Pump</Name>
<Comments />
<Quantity>1</Quantity>
<Voltage>
<Name>240 Volt 3 Phase 12w 60Hz</Name>
<RangeDescription>120/240v</RangeDescription>
<V3Id>3</V3Id>
<Value>240</Value>
<V3Value>480</V3Value>
<Connection>Delta</Connection>
<Phase>3</Phase>
<VoltageCode>J</VoltageCode>
<Active>true</Active>
<Frequency>60</Frequency>
<PowerFactor>0.8000</PowerFactor>
<UL2200>true</UL2200>
<Default>true</Default>
<TempRiseIds>
<TempRiseId>B</TempRiseId>
<TempRiseId>C</TempRiseId>
<TempRiseId>D</TempRiseId>
<TempRiseId>E</TempRiseId>
</TempRiseIds>
</Voltage>
<IsNonLinear>true</IsNonLinear>
<PriorityProperty>KWm</PriorityProperty>
<KW>0</KW>
<RunningPowerFactor xsi:nil="true" />
<StartingPowerFactor>0.90</StartingPowerFactor>
<KWm>450</KWm>
<Efficiency xsi:nil="true" />
<IsLowInertia>false</IsLowInertia>
<MotorStandard>NEMA</MotorStandard>
<MotorStartingMethod>VariableFrequencyDrive</MotorStartingMethod>
<NEMAMotorCode>F</NEMAMotorCode>
<SolidStateStarterPercent>0</SolidStateStarterPercent>
<LockedRotorKVAPerHP>5.3</LockedRotorKVAPerHP>
</Load>
<Load xsi:type="ThreePhaseMotorLoad">
<Name>Motor 450 KW Pump</Name>
<Comments />
<Quantity>1</Quantity>
<Voltage>
<Name>240 Volt 3 Phase 12w 60Hz</Name>
<RangeDescription>120/240v</RangeDescription>
<V3Id>3</V3Id>
<Value>240</Value>
<V3Value>480</V3Value>
<Connection>Delta</Connection>
<Phase>3</Phase>
<VoltageCode>J</VoltageCode>
<Active>true</Active>
<Frequency>60</Frequency>
<PowerFactor>0.8000</PowerFactor>
<UL2200>true</UL2200>
<Default>true</Default>
<TempRiseIds>
<TempRiseId>B</TempRiseId>
<TempRiseId>C</TempRiseId>
<TempRiseId>D</TempRiseId>
<TempRiseId>E</TempRiseId>
</TempRiseIds>
</Voltage>
<IsNonLinear>true</IsNonLinear>
<PriorityProperty>KWm</PriorityProperty>
<KW>0</KW>
<RunningPowerFactor xsi:nil="true" />
<StartingPowerFactor>0.90</StartingPowerFactor>
<KWm>450</KWm>
<Efficiency xsi:nil="true" />
<IsLowInertia>false</IsLowInertia>
<MotorStandard>NEMA</MotorStandard>
<MotorStartingMethod>VariableFrequencyDrive</MotorStartingMethod>
<NEMAMotorCode>F</NEMAMotorCode>
<SolidStateStarterPercent>0</SolidStateStarterPercent>
<LockedRotorKVAPerHP>5.3</LockedRotorKVAPerHP>
</Load>
</Loads>
<VoltageDip>20</VoltageDip>
</Step>
你会注意到它的不同之处在于 VoltageDip 和 Loads 的顺序不同。所以它似乎与模式中设置的序列不匹配。在用户反序列化Xml字符串之后会发生什么,Name,Loads和VoltageDip属性被设置为它们的默认值(引用类型和字符串为null,int为0)而不是序列化过程中保存的值。
使用该应用程序的每个人都使用相同的源代码。
谁能告诉我发生了什么?为什么不同用户/机器之间的Serializtion元素顺序会发生变化?为什么反序列化过程不能在它发生时处理它?</ p>
答案 0 :(得分:5)
尝试在属性的 XmlElement 属性中使用订单 对于前 -
[XmlElement(Order = 1)]
public string Prop1{get;set;}
[XmlElement(Order = 2)]
public string Prop1{get;set;}
答案 1 :(得分:2)
装饰你的xml类,尤其是order参数(另见:Other StackOverFlow question)。
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
{
/// <summary>
/// Class to represent a step in a sizing project.
/// </summary>
[Serializable]
public class Step : PSSpecObject
{
#region Fields
private int m_intVoltageDip;
private StepList m_stepList;
private LoadList m_loads;
#endregion
#region Properties
/// <summary>
/// Returns an XmlSchemaSet for this object type.
/// </summary>
[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;
}
}
}
/// <summary>
/// The Step Name
/// </summary>
[XmlElement(Order = 1)]
public override string Name
{
get
{
return base.Name;
}
set
{
base.Name = value;
}
}
/// <summary>
/// Step Voltage Dip
/// </summary>
[XmlElement(Order = 2)]
public int VoltageDip
{
get
{
return m_intVoltageDip;
}
set
{
m_intVoltageDip = value;
}
}
/// <summary>
/// Step Loads Collection
/// </summary>
[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
/// <summary>
/// Creates an instance of the class.
/// </summary>
public Step()
{
m_loads = new LoadList(this);
}
#endregion
}
}