【问题标题】:Xml Serialization Sequence IssueXml 序列化序列问题
【发布时间】:2011-03-17 17:36:46
【问题描述】:

通常所有这些都非常适合将我的对象序列化为 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>

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

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

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

【问题讨论】:

  • 大柏林代码墙。我在ToChar(60) 之后停止阅读。不知何故,你的 cmets 在那里被剥光了。

标签: c# xml serialization xml-serialization xsd


【解决方案1】:

尝试在属性的 XmlElement 属性上使用 order 对于前 -

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

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

【讨论】:

    【解决方案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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 2012-03-27
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多