开发者

deserialize object from xml gives error: 'Memory stream is not expandable'

开发者 https://www.devze.com 2023-03-28 07:36 出处:网络
i re-used some old code (i think an older .net version) which is used to deserialize an object from xml (at the end i\'ll paste the code which i used to serialize the object)

i re-used some old code (i think an older .net version) which is used to deserialize an object from xml (at the end i'll paste the code which i used to serialize the object)

this is the code:

/// <summary>开发者_运维知识库;
        /// Reconstruct an object from an XML string
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static T DeserializeObject<T>(string xml)
        {
            using (MemoryStream stream = new MemoryStream(StringToUTF8ByteArray(xml)))
            using (new XmlTextWriter(stream, Encoding.UTF8))
            {
                return (T)new XmlSerializer(typeof(T)).Deserialize(stream);
            }
        }

it gives me this error: 'Memory stream is not expandable' at the return line.

What went wrong?

this is the serialize code:

MemoryStream stream = new MemoryStream();
            using (XmlTextWriter xml = new XmlTextWriter(stream, Encoding.UTF8))
            {
                XmlSerializer xs = new XmlSerializer(item.GetType());
                xs.Serialize(xml, item);
                stream = (MemoryStream)xml.BaseStream;
            }

            return UTF8ByteArrayToString(stream.ToArray());

EDIT

this is the missing function

private static Byte[] StringToUTF8ByteArray(string pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        }


First you're creating a MemoryStream to read from the XML. You're then creating an XmlTextWriter to write to that stream. Why?

I suspect you can just get rid of the second using statement:

using (MemoryStream stream = new MemoryStream(StringToUTF8ByteArray(xml)))
{
    return (T)new XmlSerializer(typeof(T)).Deserialize(stream);
}

Having said that, it's not at all clear what your StringToUTF8ByteArray method does - is it just:

return Encoding.UTF8.GetBytes(text);

? If so, that may well not be the right thing to do - if the original XML was in some other encoding, then the XML declaration will say one thing and the stream will contain text encoded in another encoding... not good. You might be better off with:

using (TextReader reader = new StringReader(xml))
{
    return (T)new XmlSerializer(typeof(T)).Deserialize(XmlReader.Create(reader));
}


Looks like you need an XmlTextReader possibly instead of an XmlTextWriter? I am using this to serialize to a file. You might want to use something similar:

/// <summary>
/// Serialize an object into XML and save to a file
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="filePath"></param>
public static void SerializeToXml<T>(T value, string filePath) where T : class
{
    using (FileStream stream = new FileStream(filePath, FileMode.Create))
    {
        System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));
        x.Serialize(stream, value);
    }
}

/// <summary>
/// Deserialize an XML File into an object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filePath"></param>
/// <returns></returns>
public static T DeserializeFromXml<T>(string filePath) where T : class
{
    using (StreamReader stream = new StreamReader(filePath))
    {
        System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));
        return (T)x.Deserialize(stream);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消