4

2 つの単純なシリアライズ/デシリアライズの方法が必要です。

マッピング:

[System.Runtime.Serialization.DataContract(Namespace = "", Name = "PARAMS")]
    public sealed class CourseListRequest {

        [DataMember(Name = "STUDENTID")]
        public int StudentId { get; set; }

        [DataMember(Name = "YEAR")]
        public string Year { get; set; }

        [DataMember(Name = "REQUESTTYPE")]
        public int RequestType { get; set; }
    }

    public static string Serialize<T>(this T value) {
        if (value == null) throw new ArgumentNullException("value");
        try {
            var dcs = new DataContractSerializer(typeof (T));
            string xml;

            using (var ms = new MemoryStream()) {
                dcs.WriteObject(ms, value);
                xml = Encoding.UTF8.GetString(ms.ToArray());
            }
            return xml;
        }
        catch (Exception e) {
            throw;
        }
    }

    public static T Deserialize<T>(this string xml) where T : class {

        if (string.IsNullOrEmpty(xml)) {
            return default(T);
        }
        try {
            var dcs = new DataContractSerializer(typeof (T));
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml))) {
                ms.Position = 0;
                return dcs.ReadObject(ms) as T;
            }
        }
        catch (Exception e) {
            throw;
        }
    }

result:

<PARAMS xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><REQUESTTYPE>36</REQUESTTYPE><STUDENTID>0</STUDENTID><YEAR>תשע</YEAR></PARAMS>

xmlns:i="http://www.w3.org/2001/XMLSchema-instance" を削除するには?? 連載にあたって

4

1 に答える 1