1

After lot of debugging I came to know exact cause of the crash. Firstly

Test 1:

I am loading an XML file from drive deserializing it against a MOTORCLASS and using the MOTORCLASS properties, later serializing back again to XML It works fine.

Test 2:

I have a Datatable and all its rows are mapped to the MOTORCLASS properties and now when serializing to XML a crash occurs..

When looking into MOTORCLASS property

    `public object APPOINTMENT
    {
        get
        {
            return this.aPPOINTMENTField;
        }
        set
        {
            this.aPPOINTMENTField = value;
        }
    }`  

On run time the TEST 1 sets APPOINTMENT as Xmlnode whereas TEST 2 assign APPOINTMENT as Datetime.

I think if I convert Datetime to Xmlnode than it should solve the problem. But not sure how to achieve it. I have tried [System.Xml.Serialization.XmlElementAttribute("APPOINTMENT")].

But it still Datetime. Can anyone shed some light here.

TEST 1:

enter image description here TEST 2:

enter image description here

Mr.Info
  • 77
  • 2
  • 9

1 Answers1

1

The problem isn't the type of value itself that happens to be assigned to the property; It's the XmlSerializer that needs to know beforehand what type it can expect.

If APPOINTMENT should always be a DateTime, just change the property type to DateTime. Besides fixing the xml serialization problem, this will also prevent bugs and improve your application's performance.

If APPOINTMENT can be different things, you can supply type candidates to the serializer:

[XmlElement("AppointmentAsDateTime", Type = typeof(DateTime))]
[XmlElement("AppointmentAsOtherType", Type = typeof(OtherType))]
public object APPOINTMENT { get; set; }

This allows the serializer to be able to deal with situations where APPOINTMENT is a DateTime or an OtherType.

(Credit to that solution goes to Marc Gravell: How to serialize property of type Object with XmlSerializer)

Community
  • 1
  • 1
C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72