System.Runtime.Serialization.InvalidDataContractException was unhandled by user code
HResult=-2146233088
Message=Type 'xxx.Cost+RootObject' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.
Source=System.Runtime.Serialization
InnerException:
我看到另外一篇帖子,有些人建议添加数据合同和数据成员。你能帮助我在哪里可以提供数据成员和数据合同吗?我仍然没有得到它。
这是我的班级代码:
namespace xxx
{
class Cost
{
public class Title
{
public string from { get; set; }
public string to { get; set; }
public string from_zip { get; set; }
public string to_zip { get; set; }
public string from_suburb { get; set; }
public string to_suburb { get; set; }
}
public class Content
{
public string company { get; set; }
public string package { get; set; }
public string rate { get; set; }
public string rate_second { get; set; }
public string est_time { get; set; }
public string inclusion { get; set; }
public string exclusion { get; set; }
public string last_update { get; set; }
}
public class RootObject
{
public Title title { get; set; }
public List<Content> content { get; set; }
}
}
}
答案 0 :(得分:3)
您可以在http://msdn.microsoft.com/en-us/library/ms731073.aspx
阅读有关序列化合同的文档在您的情况下,您需要使用DataContract
属性注释您的课程,以及希望使用DataMember
序列化的成员,如下所示。
using System.Runtime.Serialization;
class Cost
{
[DataContract]
public class Title
{
[DataMember]
public string from { get; set; }
[DataMember]
public string to { get; set; }
[DataMember]
public string from_zip { get; set; }
[DataMember]
public string to_zip { get; set; }
[DataMember]
public string from_suburb { get; set; }
[DataMember]
public string to_suburb { get; set; }
}
[DataContract]
public class Content
{
[DataMember]
public string company { get; set; }
[DataMember]
public string package { get; set; }
[DataMember]
public string rate { get; set; }
[DataMember]
public string rate_second { get; set; }
[DataMember]
public string est_time { get; set; }
[DataMember]
public string inclusion { get; set; }
[DataMember]
public string exclusion { get; set; }
[DataMember]
public string last_update { get; set; }
}
[DataContract]
public class RootObject
{
[DataMember]
public Title title { get; set; }
[DataMember]
public List<Content> content { get; set; }
}
}