3

Normally all this works great for Serializing my object into an Xml string and back again into an object. However, I have this inconsistency between different end users that I can't seem to track down.

Basically, when I serialize an object, it looks like this once converted to 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>

However, when other random users run my program and they serialize the object, they end up with this instead:

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

You'll notice that it's different in that VoltageDip and Loads are in different sequences. So it doesn't seem to match the sequence set in the schema. What happens then after the user deserializes the Xml string, is that Name, Loads and VoltageDip properties are set to their defaults (null for reference types and strings and 0 for int) instead of the values saved in the Serialization process.

Everyone using the application is on the same exact source code.

Can anyone tell me what is happening? Why does the order of elements change on Serializtion between different users/machines? Why can't the deserialization process handles this when it occurs?

Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153
Ristogod
  • 905
  • 4
  • 14
  • 29
  • Big Berlin Wall of Code. I stopped reading after `ToChar(60)`. Somehow your comments got stripped there. – H H Mar 17 '11 at 17:49

2 Answers2

5

Try using order on the XmlElement attribute on your properties For ex -

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

[XmlElement(Order = 2)]
public string Prop1{get;set;}
Vinay B R
  • 8,089
  • 2
  • 30
  • 45
2

Decorate your xml class particularly the order parameter (also see: 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
    }
}
Community
  • 1
  • 1
Landern
  • 374
  • 2
  • 5