开发者

XML Recursive Deserialization

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:

XML Recursive Deserialization

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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜