none
怎样在Metro里做序列化和反序列化? RRS feed

答案

  • 你可以在Launch时,进行反序列化。在Suspend和Terminate时,进行序列化。

    你可以自定义数据类型,用DataContract和DataMember属性声明要序列化的类和数据成员,然后调用DataContractSerializer Class 进行实际的序列化和反序列化。

    样例代码如下

    using System.Runtime.Serialization;
    
    // declare custom serializable type
    
    [DataContract(Name = "MyData", Namespace = "http://www.mysite.com")]
    class MyData
    {
    	[DataMember()]
    	public string FirstName;
    	[DataMember]
    	public string LastName;
    	[DataMember()]
    	public int ID;
    
    	public MyData(string fname, string lname, int newID)
    	{
    		FirstName = fname;
    		LastName = lname;
    		ID = newID;
    	}
    }
    
    // serialize sample code
    
    MyData myData = new MyData("a", "b", 1);             
    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("mydata,xml", CreationCollisionOption.ReplaceExisting);	
    IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite);
    using (IOutputStream outStream = stream.GetOutputStreamAt(0))
    {                                              
    	DataContractSerializer serializer = new DataContractSerializer(typeof(MyData));
    	serializer.WriteObject(outStream.AsStreamForWrite(), myData);
    	await outStream.FlushAsync();
    }
    			


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年3月18日 7:22
    版主

全部回复

  • 你可以在Launch时,进行反序列化。在Suspend和Terminate时,进行序列化。

    你可以自定义数据类型,用DataContract和DataMember属性声明要序列化的类和数据成员,然后调用DataContractSerializer Class 进行实际的序列化和反序列化。

    样例代码如下

    using System.Runtime.Serialization;
    
    // declare custom serializable type
    
    [DataContract(Name = "MyData", Namespace = "http://www.mysite.com")]
    class MyData
    {
    	[DataMember()]
    	public string FirstName;
    	[DataMember]
    	public string LastName;
    	[DataMember()]
    	public int ID;
    
    	public MyData(string fname, string lname, int newID)
    	{
    		FirstName = fname;
    		LastName = lname;
    		ID = newID;
    	}
    }
    
    // serialize sample code
    
    MyData myData = new MyData("a", "b", 1);             
    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("mydata,xml", CreationCollisionOption.ReplaceExisting);	
    IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite);
    using (IOutputStream outStream = stream.GetOutputStreamAt(0))
    {                                              
    	DataContractSerializer serializer = new DataContractSerializer(typeof(MyData));
    	serializer.WriteObject(outStream.AsStreamForWrite(), myData);
    	await outStream.FlushAsync();
    }
    			


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年3月18日 7:22
    版主
  •           下面是c#例子 你可以参考看看!

           StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
                IRandomAccessStream raStream = await file.OpenAsync(FileAccessMode.ReadWrite);
                using (IOutputStream outStream = raStream.GetOutputStreamAt(0))
                {
                    XmlSerializerHelper.Serialize(outStream.AsStreamForWrite(), _dictionaries);
                }
         
              StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
                    if (file == null) return;
                    IInputStream inStream = await file.OpenSequentialReadAsync();
                    _dictionaries = (DictCache)XmlSerializerHelper.Deserialize(inStream.AsStreamForRead(), typeof(DictCache));

    2012年4月18日 13:51