tags:

views:

44

answers:

4

I have the following extension method to serialize my class....

        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();

        }

This seems to work fine and returns the following string for my serialized object:

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

However when I try to deserialize this string using the method below I get this error:

{"The encoding style '<AuthenticatedUser xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"&gt;\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."}

Method....

 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);
        }
    }

So after I read the error message I changed the deserialize method to use the 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);
        }
    }

However I now get this error...

{"The encoding style '<AuthenticatedUser xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"&gt;\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."}

Does anyone know where I'm going wrong and how I can deserialize this string successfully?

A: 

XElement.CreateReader() doesn't return the XDeclaration.

Instead, try making an XmlReader from a StringReader.

SLaks
@SLaks - Thanks for the suggestion but I've just tried it and I'm still get the same error?
MarkB29
A: 

Do you need the Parse(xml) call and the reader element? Since you have the string, can't you just deserialize the string? First convert to bytes...

byte [] bytes = Encoding.Unicode.GetBytes(xml);
MemoryStream mem = new MemoryStream(bytes);
returnValue = (T)ser.Deserialize(mem);
Les
You can make an `XmlReader` around a `StringReader`
SLaks
A: 

Why are you using XmlSerializer ?

Unless you must control the way the output XML looks, you should be using DataContractSerializer

Here is a nice blog post about the two

ohadsc
A: 

The problem is the overload of the Deserialize method that you are calling:

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

The xml parameter in the call specifies the encoding style, but in this case you are passing the xml from the serialization. Simply delete the second parameter and just call Deserialize with the reader and it should work fine:

            return (T)ser.Deserialize(reader);
Steve Ellinger