Search code examples
c#xmlofx

Deserialising XML doc throwing errors - how to find out where it is failing


I am trying to deserialize an OFX xml document into its object (I converted the XSD's to classes)

Here is the code below.

  XmlSerializer xmlSerializer = new XmlSerializer(typeof(OFX));

        using (var reader = xmlDoc.Root.CreateReader())
        {
            return (OFXRequest)xmlSerializer.Deserialize(reader);
        }

It fails on newing up of the xml XmlSerializer with the error

The xml doc:

<?OFX OFXHEADER=200  VERSION=211  SECURITY="NONE"  OLDFILEUID="NONE"  NEWFILEUID="NONE"  ?>
<OFX>
  <SIGNONMSGSRQV1>
    <SONRQ>
      <DTCLIENT>20120202</DTCLIENT>
      <USERID>USER-ID</USERID>
      <USERPASS>PASSWORD</USERPASS>
      <LANGUAGE>ENG</LANGUAGE>
      <FI>
        <ORG>Organisation</ORG>
        <FID>OrganisationID</FID>
      </FI>
      <APPID>YOD</APPID>
      <APPVER>1</APPVER>
    </SONRQ>
  </SIGNONMSGSRQV1>
  <SIGNUPMSGSRQV1>
    <ACCTINFOTRNRQ>
      <TRNUID>456579841231</TRNUID>
      <ACCTINFORQ>
        <DTACCTUP>2013101209000.000[2:GMT]</DTACCTUP>
      </ACCTINFORQ>
    </ACCTINFOTRNRQ>
  </SIGNUPMSGSRQV1>
</OFX>

The Error:

Unable to generate a temporary class (result=1). error CS0030: Cannot convert type 'System.DateTime' to 'string' error CS0030: Cannot convert type 'System.DateTime' to 'string' error CS0030: Cannot convert type 'System.DateTime' to 'string'

What I need do know is how do I find the exact place in code that is failing whilst deserializing? There is no inner exception etc.


Solution

  • At a guess, it is confused what you want it do do with things like this

    [XmlElement(Type=typeof(string),ElementName="TAXYEAR",IsNullable=false,DataType="gYear")]
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    public TAXYEARCollection __TAXYEARCollection;
    

    which clearly says "treat the values as a string", verses this:

    [Serializable]
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    public class TAXYEARCollection : ArrayList
    {
        public DateTime Add(DateTime obj)
        {
            base.Add(obj);
            return obj;
        }
    
        public DateTime Add()
        {
            return Add(new DateTime());
        }
    
        public void Insert(int index, DateTime obj)
        {
            base.Insert(index, obj);
        }
    
        public void Remove(DateTime obj)
        {
            base.Remove(obj);
        }
    
        new public DateTime this[int index]
        {
            get { return (DateTime) base[index]; }
            set { base[index] = value; }
        }
    }
    

    which clearly wants values to be a DateTime.