Using DataContractSerializer to serialize, but can't deserialize back

前端 未结 4 960
醉话见心
醉话见心 2020-12-02 06:44

I have the following 2 functions:

public static string Serialize(object obj)
{
    DataContractSerializer serializer = new DataContractSerializer(obj.GetType         


        
相关标签:
4条回答
  • 2020-12-02 06:52

    I ended up doing the following and it works.

    public static string Serialize(object obj)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
            serializer.WriteObject(memoryStream, obj);
            return Encoding.UTF8.GetString(memoryStream.ToArray());
        }
    }
    
    public static object Deserialize(string xml, Type toType)
    {
        using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
        {
            XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, Encoding.UTF8, new XmlDictionaryReaderQuotas(), null);
            DataContractSerializer serializer = new DataContractSerializer(toType);
            return serializer.ReadObject(reader);
        }
    }
    

    It seems that the major problem was in the Serialize function when calling stream.GetBuffer(). Calling stream.ToArray() appears to work.

    0 讨论(0)
  • 2020-12-02 06:54

    Here is how I've always done it:

        public static string Serialize(object obj) {
            using(MemoryStream memoryStream = new MemoryStream())
            using(StreamReader reader = new StreamReader(memoryStream)) {
                DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
                serializer.WriteObject(memoryStream, obj);
                memoryStream.Position = 0;
                return reader.ReadToEnd();
            }
        }
    
        public static object Deserialize(string xml, Type toType) {
            using(Stream stream = new MemoryStream()) {
                byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
                stream.Write(data, 0, data.Length);
                stream.Position = 0;
                DataContractSerializer deserializer = new DataContractSerializer(toType);
                return deserializer.ReadObject(stream);
            }
        }
    
    0 讨论(0)
  • 2020-12-02 07:02

    Other solution is:

    public static T Deserialize<T>(string rawXml)
    {
        using (XmlReader reader = XmlReader.Create(new StringReader(rawXml)))
        {
            DataContractSerializer formatter0 = 
                new DataContractSerializer(typeof(T));
            return (T)formatter0.ReadObject(reader);
        }
    }
    

    One remark: sometimes it happens that raw xml contains e.g.:

    <?xml version="1.0" encoding="utf-16"?>

    then of course you can't use UTF8 encoding used in other examples..

    0 讨论(0)
  • 2020-12-02 07:18

    This best for XML Deserialize

     public static object Deserialize(string xml, Type toType)
        {
    
            using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                System.IO.StreamReader str = new System.IO.StreamReader(memoryStream);
                System.Xml.Serialization.XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(toType);
                return xSerializer.Deserialize(str);
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题