XmlSerializer - 不支持编码

时间:2010-10-17 12:18:51

标签: c# .net

我有以下扩展方法来序列化我的类......

        public static string SerializeToXml<T>(this object obj)
        {
            XDocument doc = new XDocument();         

            XmlSerializer ser = new XmlSerializer(typeof(T));

            using (var writer = doc.CreateWriter())
            {
                ser.Serialize(writer, obj);
            }

            return doc.ToString();

        }

这似乎工作正常,并为我的序列化对象返回以下字符串:

<AuthenticatedUser xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Username>mark</Username>
  <UserID>4</UserID>
  <Roles>
    <string>AuthenticatedUsers</string>
  </Roles>
  <IsValid>false</IsValid>
</AuthenticatedUser>

但是,当我尝试使用下面的方法反序列化此字符串时,我收到此错误:

{"The encoding style '<AuthenticatedUser xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n  <Username>mark</Username>\r\n  <UserID>4</UserID>\r\n  <Roles>\r\n    <string>AuthenticatedUsers</string>\r\n  </Roles>\r\n  <IsMale>false</IsMale>\r\n</AuthenticatedUser>' is not valid for this call because this XmlSerializer instance does not support encoding. Use the SoapReflectionImporter to initialize an XmlSerializer that supports encoding."}

...方法

 public static T DeserializeFromXml<T>(this string xml)
    {
        var element = XElement.Parse(xml);

        XmlSerializer ser = new XmlSerializer(typeof(T));

        using (var reader = element.CreateReader())
        {                
            return (T)ser.Deserialize(reader, xml);
        }
    }

因此,在我阅读错误消息后,我更改了deserialize方法以使用SoadReflectionImporter ....

 public static T DeserializeFromXml<T>(this string xml)
    {
        var element = XElement.Parse(xml);

        SoapReflectionImporter soap = new SoapReflectionImporter();
        var mapping = soap.ImportTypeMapping(typeof(T));
        XmlSerializer ser = new XmlSerializer(mapping);

        using (var reader = element.CreateReader())
        {                
            return (T)ser.Deserialize(reader, xml);
        }
    }

但是我现在得到这个错误......

{"The encoding style '<AuthenticatedUser xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n  <Username>mark</Username>\r\n  <UserID>4</UserID>\r\n  <Roles>\r\n    <string>AuthenticatedUsers</string>\r\n  </Roles>\r\n  <IsValid>false</IsValid>\r\n</AuthenticatedUser>' is not valid for this call. Valid values are 'http://schemas.xmlsoap.org/soap/encoding/' for SOAP 1.1 encoding or 'http://www.w3.org/2003/05/soap-encoding' for SOAP 1.2 encoding."}

有谁知道我哪里出错了,以及如何成功地反序列化这个字符串?

4 个答案:

答案 0 :(得分:4)

问题是您正在调用的Deserialize方法的重载:

            return (T)ser.Deserialize(reader, xml);

调用中的xml参数指定编码样式,但在这种情况下,您将从序列化传递xml。只需删除第二个参数,然后使用阅读器调用反序列化,它应该可以正常工作:

            return (T)ser.Deserialize(reader);

答案 1 :(得分:0)

XElement.CreateReader()不会返回XDeclaration

相反,请尝试从XmlReader创建StringReader

答案 2 :(得分:0)

您需要Parse(xml)调用和reader元素吗?既然你有字符串,你不能只是反序列化字符串吗?首先转换为字节...

byte [] bytes = Encoding.Unicode.GetBytes(xml);
MemoryStream mem = new MemoryStream(bytes);
returnValue = (T)ser.Deserialize(mem);

答案 3 :(得分:0)

为什么使用XmlSerializer?

除非您必须控制输出XML的外观,否则您应该使用DataContractSerializer

这是关于两个

nice blog post