XML递归反序列化

时间:2011-06-15 13:27:33

标签: c# xml serialization xml-serialization

我有一些我试图反序列化的XML。 Kronos_WCF对象反序列化很好,但Response对象没有。是否有一些我遗漏的递归反序列化teqnique?

以下是我尝试反序列化的XML:

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

这是我的解串器:

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;
}

以下是我在VS2010中看到的屏幕截图:

enter image description here

以下是使用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;
        }
    }
}

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

xml的Response属性似乎与您班级中的响应属性不匹配。

答案 1 :(得分:1)

Status中定义的

Response具有错误的属性

应该是

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

而实际上它是

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

所以xml反序列化器正在寻找

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

这至少可以让您反序列化Response.Status

看起来xml片段与类定义不匹配。