2

I have some XML that I am trying to deserialize. The Kronos_WCF object deserializes fine but the Response objects do not. Is there some recursive deserialization teqnique I am missing?

Here is the XML I am trying to deserialize:

<?xml version='1.0' encoding='UTF-8' ?>
<Kronos_WFC Version="1.0" WFCVersion="6.2.0.4" TimeStamp="6/15/2011 9:15AM GMT-04:00">
<Response Status="Success" Timeout="1800" PersonKey="-1" Object="System" UserName="User" Action="Logon" PersonNumber="User">
</Response>
<Response Status="Success" Object="System" UserName="User" Action="Logoff">
</Response>
</Kronos_WFC>

Here is my deserializer:

public static T Deserialize<T>(this string xml)
{
    var ser = new XmlSerializer(typeof (T));
    object obj;
    using (var stringReader = new StringReader(xml))
    {
        using (var xmlReader = new XmlTextReader(stringReader))
        {
            obj = ser.Deserialize(xmlReader);
        }
    }
    return (T) obj;
}

Here is a screen shot of what I am seeing in VS2010:

enter image description here

Here is the code from the classes generated using XSD.exe:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlRootAttribute("Kronos_WFC", Namespace = "", IsNullable = false)]
public class Kronos_WFCType
{

    private object[] m_itemsField;

    private string m_timeStampField;

    private string m_versionField;

    private string m_wFcVersionField;

    /// <remarks/>
    [XmlElementAttribute("Request", typeof(RequestType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [XmlElementAttribute("Response", typeof(ResponseType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [XmlElementAttribute("Transaction", typeof(TransactionType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public object[] Items
    {
        get
        {
            return m_itemsField;
        }
        set
        {
            m_itemsField = value;
        }
    }

    [XmlAttributeAttribute()]
    public string TimeStamp
    {
        get
        {
            return m_timeStampField;
        }
        set
        {
            m_timeStampField = value;
        }
    }

    /// <remarks/>
    [XmlAttributeAttribute()]
    public string Version
    {
        get
        {
            return m_versionField;
        }
        set
        {
            m_versionField = value;
        }
    }

    /// <remarks/>
    [XmlAttributeAttribute()]
    public string WFCVersion
    {
        get
        {
            return m_wFcVersionField;
        }
        set
        {
            m_wFcVersionField = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public class ResponseType
{

    private string messageField;

    private string sequenceField;

    private string statusField;

    private string transactionSequenceField;

    /// <remarks/>
    [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Message
    {
        get
        {
            return messageField;
        }
        set
        {
            messageField = value;
        }
    }

    /// <remarks/>
    [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Sequence
    {
        get
        {
            return sequenceField;
        }
        set
        {
            sequenceField = value;
        }
    }

    /// <remarks/>
    [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Status
    {
        get
        {
            return statusField;
        }
        set
        {
            statusField = value;
        }
    }

    /// <remarks/>
    [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string TransactionSequence
    {
        get
        {
            return transactionSequenceField;
        }
        set
        {
            transactionSequenceField = value;
        }
    }
}

Any help would be appreciated.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Cyberdrew
  • 1,832
  • 1
  • 19
  • 39

2 Answers2

1

The Response attributes from the xml does not seem to match those present in your class.

Bala R
  • 107,317
  • 23
  • 199
  • 210
1

Status defined in Response has the wrong Attribute

it should be

[XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Status

and instead it is actually

[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]

so the xml deserializer is looking for

<Response><Status>Success</Status></Response>

This will at least allow you to deserialize Response.Status

It doesnt look like that xml snippet matches with the class definition.

wal
  • 17,409
  • 8
  • 74
  • 109
  • I generated this from an XSD supplied by the API vendor. Is there a way to set this when generating the classes using XSD.exe or does it need to just be manually changed? Thanks. – Cyberdrew Jun 15 '11 at 14:42
  • 1
    If the xsd is from a vendor and the xml response is from the same vendor's product, I'd talk to the vendor about it. – Bala R Jun 15 '11 at 14:44
  • 1
    @Cyberdrew re: XSD I do not know. Instead, i would modify the supplied XSD to match the xml you're receiving so the correct classes *are* generated. I would also ask the vendor why there is a mismatch in the XSD and the sample xml. – wal Jun 15 '11 at 14:45
  • Thanks, will do. Thanks for the help. Pardon me being blunt but the API for this product is crap. – Cyberdrew Jun 15 '11 at 14:55