c# – Xml序列化序列问题

c# – Xml序列化序列问题,第1张

概述通常,这一切都非常适合将我的对象序列化为Xml字符串并再次返回到对象中.但是,我有不同的最终用户之间的这种不一致,我似乎无法追查. 基本上,当我序列化一个对象时,它看起来像转换为Xml后: <?xml version="1.0" encoding="utf-8"?><Step xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns: 通常,这一切都非常适合将我的对象序列化为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元素顺序会发生变化?为什么反序列化过程在发生时不能处理?

解决方法 尝试在属性的XmlElement属性上使用顺序
对于前 –

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

以上是内存溢出为你收集整理的c# – Xml序列化序列问题全部内容,希望文章能够帮你解决c# – Xml序列化序列问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1216722.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存