datacontractserializer序列化反序列化列表总是空的 [英] datacontractserializer deserialize list<> always empty

查看:158
本文介绍了datacontractserializer序列化反序列化列表总是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在反序列化从Web服务接收的数据.

I'm deserializing data received from a web-service.

问题是List的反序列化返回一个空列表,并且没有生成异常.你能帮我弄清楚为什么吗? 我们尝试了几种可能的语法.下面的代码是最接近正确解决方案的代码,但是我们不能正确地反序列化为类列表.

The problem is that the deserialization of List returns an empty list and no exception are generated. Can you help me figure out why? We have tried several possible syntax. The code below is the closest to the correct solution but we cannot deserialize correctly as a list of classes.

<ArrayOfBatch xmlns="http://schemas.datacontract.org/2004/07/myns" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

<MaasBatch>
<BatchName>All Users</BatchName>
<Users>
<MaasUsers>
    <firstName>bob</firstName>
    <lastName>thetest</lastName>        
    <sourceEmail>bob@source.com</sourceEmail>
    <sourceTenantID>111</sourceTenantID>
    <targetEmail>bob@target.com</targetEmail>
    <targetTenantID>222</targetTenantID>
</MaasUsers>
</Users>
</MaasBatch>
</ArrayOfBatch>

代码:

 List<MAASBatch> lstMaasBatches = null;
        try
        {                
            string target = string.Empty;
            using (var response = request.GetResponse())
            {
                Stream streamReader = response.GetResponseStream();
                DataContractSerializer serializer = new DataContractSerializer(typeof(List<MAASBatch>));
                lstMaasBatches = (List<MAASBatch>)serializer.ReadObject(streamReader);
                streamReader.Close();                   
            }
            return lstMaasBatches;
        }
        catch (Exception exc)
        {
            return lstMaasBatches;
        }    

班级:

 [DataContract(Name = "MAASBatch", Namespace = "http://schemas.datacontract.org/2004/07/myns")]
[KnownType(typeof(MAASUsers))]
public class MAASBatch
{
    [DataMember]
    public string BatchName { get; set; }
    [DataMember]
    public List<MAASUsers> Users { get; set; }

    [OnDeserializing]
    internal void OnDeserializingCallBack(StreamingContext streamingContext)
    {
        this.Users = new List<MAASUsers>();
    }
}

[DataContract(Name = "MAASUsers", Namespace = "http://schemas.datacontract.org/2004/07/myns")]    
public class MAASUsers
{
     [DataMember]
    public string firstName { get; set; }
     [DataMember]
    public string lastName { get; set; }
     [DataMember]
    public string sourceEmail { get; set; }
     [DataMember]
    public int sourceAgentID { get; set; }
     [DataMember]
    public string targetEmail { get; set; }
     [DataMember]
    public int targetAgentID { get; set; }

}

推荐答案

数据协定元素的名称为"MAASUsers",但在xml中,该元素的名称为"MaasUsers".数据协定序列化程序区分大小写,因此不会将两者匹配.

The data contract element name is "MAASUsers" but in the xml the element is named "MaasUsers". The data contract serializer is case sensitive so it will NOT match these two up.

这篇关于datacontractserializer序列化反序列化列表总是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆