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

1 に答える 1

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; }
    }
}
于 2013-03-02T23:19:27.860 に答える