DataContractJsonSerializer无法正确反序列化,缺少某些属性值

时间:2012-09-11 03:21:43

标签: c# xml wcf

响应xml如下所示,您会看到xml的属性按字母顺序排列:

 <ResponseItem xmlns="http://schemas.datacontract.org/2004/07/WebRole" 
               xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
         <Error>
                 <ErrorCode>101</ErrorCode>
                 <ErrorMessage>Parameter cannot be null or empty.</ErrorMessage>
         </Error>
         <ResponseContent i:nil="true" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
 </ResponseItem>

在客户端,DataContract位于以下位置:

public class Error
{
    [DataMember]
    public string ErrorCode { get; set; }
    [DataMember]
    public string ErrorMessage { get; set; }
}

[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/WebRole")]
public class ResponseItem
{
    [DataMember]
    public Error Error { get; set; }
    [XmlArrayItem(typeof(string), Namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays")]
    [DataMember]
    public string[] ResponseContent { get; set; }
}

以下是反序列化代码:

    ResponseItem GetObject(Stream stream)
    {
        //get xml format message
        //StreamReader reader = new StreamReader(stream, Encoding.UTF8);
        //string responseStr = reader.ReadToEnd();
        //Console.Out.Write(responseStr);

        //get return object
        DataContractSerializer obj = new DataContractSerializer(typeof(ResponseItem));
        ResponseItem item = obj.ReadObject(stream) as ResponseItem;
        return item;
    } 

使用DataContractSerializer将xml反序列化为对象后,errorcode和errormessage为空!为什么?谁能告诉我?在此先感谢!!

0 个答案:

没有答案