How to use binary in Windows Phone 7

How can I use BinaryFormatter in Windows Phone 7.

I use this one using System.Runtime.Serialization.Formatters.Binaryin service1.svc.cs, but I can’t use this link in a Windows 7 phone.

Is there any solution for this?

My code for DeserializeObject

 public static T DeserializeObject<T>(byte[] xml)
    {
        BinaryFormatter xs = new BinaryFormatter();
        MemoryStream memoryStream = new MemoryStream(xml);
        return (T)xs.Deserialize(memoryStream);
    } 

BinaryFormatter gives an error in a Windows 7 phone. So, how can I Deserialize . What changes should I make in this code?

+5
source share
3 answers

@driis, BinaryFormatter Windows Phone. WCF (.. , BinaryMessageEncodingBindingElement HttpTransportBindingElement), WP7. .

: , , - , , Silverlight. DataContractSerializer, /, , . , , SL-:

public static T DeserializeObject<T>(byte[] xml) 
{ 
    using (MemoryStream memoryStream = new MemoryStream(xml))
    {
        using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(
            memoryStream, XmlDictionaryReaderQuotas.Max))
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(T));
            return (T)dcs.ReadObject(reader);
        }
    }
}

:

public static byte[] SerializeObject<T>(T obj)
{
    using (MemoryStream ms = new MemoryStream())
    {
        using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(ms))
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(T));
            dcs.WriteObject(writer, obj);
            writer.Flush();
            return ms.ToArray();
        }
    }
}
+6

BinaryFormatter Windows Phone 7.

+2

There is no library support for binary serialization in WP 7.1. as mentioned in driis.

Instead, you should use XmlObjectSerializeror even one of its subclasses that support serialization of the most common API objects, such as contact data, etc.

If you System.Runtime.Serializationchecked the namespace assembly (for example, in the Visual Studio Object Browser), you discovered a hierarchy of the corresponding classes.

And why do you care about serialization method? XML serialization is more portable, more uniform, and human-readable.

0
source

All Articles