Example #1
0
    /// <summary>
    /// 演示DataContractSerializer的序列化和反序列化
    /// </summary>
    void ShowDataContractSerializer()
    {
        var dataContractSerializerObject = new API.DataContractSerializerObject {
            ID = Guid.NewGuid(), Name = "DataContractSerializer", Age = 28, Time = DateTime.Now
        };

        var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(API.DataContractSerializerObject));

        // 序列化
        var ms = new MemoryStream();

        serializer.WriteObject(ms, dataContractSerializerObject);

        ms.Position = 0;
        var sr  = new StreamReader(ms);
        var str = sr.ReadToEnd();

        txtDataContractSerializer.Text = str;


        // 反序列化
        var buffer = System.Text.Encoding.UTF8.GetBytes(str);
        var ms2    = new MemoryStream(buffer);
        var dataContractSerializerObject2 = serializer.ReadObject(ms2) as API.DataContractSerializerObject;

        lblDataContractSerializer.Text = dataContractSerializerObject2.Name;
    }
Example #2
0
    /// <summary>
    /// 演示DataContractSerializer的序列化和反序列化
    /// </summary>
    void ShowDataContractSerializer()
    {
        var dataContractSerializerObject = new API.DataContractSerializerObject { ID = Guid.NewGuid(), Name = "DataContractSerializer", Age = 28, Time = DateTime.Now };

        var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(API.DataContractSerializerObject));

        // 序列化
        var ms = new MemoryStream();

        serializer.WriteObject(ms, dataContractSerializerObject);

        ms.Position = 0;
        var sr = new StreamReader(ms);
        var str = sr.ReadToEnd();
        txtDataContractSerializer.Text = str;

        // 反序列化
        var buffer = System.Text.Encoding.UTF8.GetBytes(str);
        var ms2 = new MemoryStream(buffer);
        var dataContractSerializerObject2 = serializer.ReadObject(ms2) as API.DataContractSerializerObject;
        lblDataContractSerializer.Text = dataContractSerializerObject2.Name;
    }