数据合同序列化错误 [英] Datacontract serialization error

查看:120
本文介绍了数据合同序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 如何在所有类成员上设置[DataMember]

所以我必须使用字典和其他成员序列化一个类.

So I have to serialize a class with dictionaries and other members.

我选择了本身的数据上下文序列化

I have chonse the datacontext serialization that se

public SimpleDataGridSample()
    {
      if (false)
      {

        MyClass theclass = new MyClass();

        var serializer = new DataContractSerializer(typeof(MyClass));

        using (Stream fileStream = File.Open("aaa.bin", FileMode.Create))
        {
          XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(fileStream);
          serializer.WriteObject(binaryDictionaryWriter, theclass);
          binaryDictionaryWriter.Flush();
        }
      }
      else
      {
        MyClass theclass;
        var serializer = new DataContractSerializer(typeof(MyClass));

        using (Stream fileStream = File.Open("aaa.bin", FileMode.Open))
        {
          XmlDictionaryReaderQuotas xq = new XmlDictionaryReaderQuotas();
          XmlDictionaryReader binaryDictionarReader = XmlDictionaryReader.CreateBinaryReader(fileStream, xq);
          theclass = (MyClass)serializer.ReadObject(binaryDictionarReader);

        }
      }

    }
  }

那行得通.

但这只是一个测试程序. 在申请更复杂的课程时,出现此错误:

But that was just a test program. When applying to my class which is more complicated I get this error:

{"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}

不明白索引在说什么.

主类由服务器成员(也包括字典和可观察列表)和其他子类组成. 每个班级都标记为[DataContract(IsReference = true)] 并且每个成员都标记为[DataContext]

The main class is made of serveral members (also dictionaries and observable lists) and other sub classes. Every class is marked [DataContract(IsReference = true)] and every member is marked [DataContext]

感谢

推荐答案

其他用户无法进行查询的确定.最初,我不知道问题出在哪里,因为我以前从未使用过DataContract序列化,而且对此还不熟.但这有效! 使我步入正轨的是古老的智能感知能力. 这是创建后的变量,它包含异常. 一样谢谢你

OK that was IMPOSSIBLE for other users to aswer. At first I didn't understand where the problem was because I never used used DataContract serialization before and was not proficent about it. But it works! What put me on track was the good old intellisense. This is the variable after having been created and it contains the exception. Thank you the same

这篇关于数据合同序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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