Windows Phone 7 Serialization: DataContract Serialization

转自:http://www.devdiv.com/forum.php?mod=viewthread&tid=56922&extra=page%3D1%26filter%3Dtypeid%26typeid%3D305%26typeid%3D305

 

 

[p=24, null, left]In this post I am going to continue studying the ways how to serialize objects in Windows Phone 7 applications. This time I will explain DataContract Serialization using DataContractSerializer class. This type of serialization can also be applied to Silverlight applications.

[p=24, null, left]

Additional Information Adding a reference[p=24, null, left]Keep in mind that DataContractSerializer requires System.Runtime.Serialization namespace to be added to project. To do so right click the project name and choose Add Reference there. Select System.Runtime.Serialization namespace on .NET tab (check image bellow).


[p=24, null, left] 

Creating a sample classI am going to use almost the same class I’ve created for theXmlSerializer tutorial, but slightly modified. To allow object to be serialized using DataContract serialization you have to addDataContractAttribute to the class (public class). XML Generation can be controlled usingDataMemberAttribute for those properties that need to be serialized andIgnoreDataMemberAttribute for those properties that need to be ignored during serialization. Public properties will be automatically exposed. Take a look at example bellow.

  1. [DataContractAttribute]
  2. public class SampleData
  3. {
  4.     [DataMember]
  5.     public string ContentText { get; set; }

  6.     [DataMember]
  7.     public List<int> SomeItems { get; set; }

  8.     public SampleData()
  9.     {
  10.         ContentText = "some text";
  11.         SomeItems = new List<int>() { 1, 2, 3 };
  12.     }
  13. }
复制代码
SerializationTo serialize an object you need to create an instance of DataContractSerializer class (passing a type of serialized object as an input parameter) and then write serialized data to a stream object using WriteObject method, that accepts stream and object marked with DataContractAttribute attribute as input parameters.
  1. public static void Serialize(Stream streamObject, object objForSerialization)
  2. {
  3.     if (objForSerialization == null || streamObject == null)
  4.         return;

  5.     DataContractSerializer ser = new DataContractSerializer(objForSerialization.GetType());
  6.     ser.WriteObject(streamObject, objForSerialization);
  7. }
复制代码

DeserializationDeserialization is also very simple. To deserialize an object you need to create an instance of DataContractSerializer class again (pass constructor a type of serialized object) and then read serialized data out of a stream object using ReadObject method, that accepts stream object as parameter.
  1. public static object Deserialize(Stream streamObject, Type serializedObjectType)
  2. {
  3.     if (serializedObjectType == null || streamObject == null)
  4.         return null;

  5.     DataContractSerializer ser = new DataContractSerializer(serializedObjectType);
  6.     return ser.ReadObject(streamObject);
  7. }
复制代码

TestingI have used the following method to test serialization and deserialization using DataContractSerializer. As the result instance of SampleData class after deserialization is exactly the same as the one before the serialization.
  1. public static void Test()
  2. {
  3.     // serialization
  4.     MemoryStream ms = new MemoryStream();
  5.     DataContractSerializationHelper.Serialize(ms, new SampleData());

  6.     ms.Position = 0;
  7.     // deserialization
  8.     var sampleData = DataContractSerializationHelper.Deserialize(ms, typeof(SampleData));

  9.     ms.Close();
  10. }
复制代码

Here is how serialized instance of SampleData class looks like:
  1. <SampleData xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
  2. xmlns="http://schemas.datacontract.org/2004/07/EugeneDotnetWPSamples.Serialization.DataContract">
  3. <ContentText>some text</ContentText>
  4. <SomeItems xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  5. <d2p1:int>1</d2p1:int>
  6. <d2p1:int>2</d2p1:int>
  7. <d2p1:int>3</d2p1:int>
  8. </SomeItems>
  9. </SampleData>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值