1

im try to deserialize the following xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<common:messages xmlns:xlink="http://www.w3.org/1999/xlink" 
                 xmlns:ns3="http://rest.immobilienscout24.de/schema/platform/gis/1.0" 
                 xmlns:common="http://rest.immobilienscout24.de/schema/common/1.0">
  <message>
    <messageCode>ERROR_DESCRIPTION</messageCode>
    <message>The request is not schema valid. [MESSAGE: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 3; cvc-complex-type.2.4.a: Invalid content was found starting with element 'external-Id'. One of '{externalId, title}' is expected.] </message>
  </message>
  <message>
      ...
  </message>
</common:messages>

into following messages class instance:

[XmlRoot("messages", Namespace = NS.Common)]
public partial class messages
{
    [XmlElementAttribute("message")]
    public List<Message> message { get; set;}
}

public partial class Message
{
    [XmlElement("messageCode", Namespace = NS.Common)]
    public MessageCode messageCode { get; set; }

    [XmlElement("message", Namespace = NS.Common)]
    public string message { get; set; }
}

[Serializable]
[XmlType(Namespace = NS.Common)]
public enum MessageCode
{
    ERROR_DESCRIPTION,
    RESOURCE_CREATED
}


public class NS
{
    public const string Common = "http://rest.immobilienscout24.de/schema/common/1.0";
}

public class XmlHelper
{
    public static T Deserialize<T>(string xml, Encoding encoding = null) where T : new()
    {
            if (xml == string.Empty)
                return new T();

            var serializer = new XmlSerializer(typeof(T));
            encoding = encoding ?? Encoding.UTF8;
            using (var stream = new MemoryStream(encoding.GetBytes(xml)))
                return (T)serializer.Deserialize(stream);
    }
}

I get no errors from XmlSerializer, but the message collection is always empty.

I have a suspection the XmlSerializer can't properly resolve what is the "message" - an array item or an element containing the string message. I can't change the xml structure, so i must fix my deserializer routine or some xml property attributes.

What is wrong with my code? Any help appreciated.

python_kaa
  • 1,034
  • 13
  • 27
  • I tried getting it to work but couldn't. One thing I stumpled upon was the use of `XmlElement` instead of `XmlElementAttribute` for the message elements (see details here: http://stackoverflow.com/questions/11731947/xml-serialization-of-list) – meilke Sep 23 '13 at 09:45
  • There is no difference. If you or .NET base library has some StrangeNamedAttribute you can use it either as `[StrangeNamed]` or `[StrangeNamedAttribute]`. And thank you. im aware of the upproach desrcibed in the answer you references and read it and some others before I posted my own question. – python_kaa Sep 23 '13 at 10:22
  • You are absolutely right! My mistake! I guess I was confused because of attributes used in C# and XML... – meilke Sep 23 '13 at 10:45

1 Answers1

0

I guess your issue is with namespaces. As messages seems the only element bearing the common namespace, you may try to reset the namespace for all child elements. Something like this :

[XmlRoot("messages", Namespace = NS.Common)]
public partial class messages
{
    [XmlElementAttribute("message", Namespace = "")]
    public List<Message> message { get; set;}
}

public partial class Message
{
    [XmlElement("messageCode", Namespace = "")]
    public MessageCode messageCode { get; set; }

    [XmlElement("message", Namespace = "")]
    public string message { get; set; }
}

[Serializable]
[XmlType(Namespace = "")]
public enum MessageCode
{
    ERROR_DESCRIPTION,
    RESOURCE_CREATED
}

or with Form = System.Xml.Schema.XmlSchemaForm.Unqualified

python_kaa
  • 1,034
  • 13
  • 27
jbl
  • 15,179
  • 3
  • 34
  • 101
  • Yes it has worked! If i serialize the instance again, the upper `` becomes ``. I have also experimented with `Namespace = NS.Common, Form = XmlSchemaForm.Unqualified` with exactly serialized and deserialized results. – python_kaa Sep 23 '13 at 10:16
  • @meilke good point. The String.Empty in attributes do not compile. I replaced them with empty strings. thx – jbl Sep 23 '13 at 11:00
  • @python_kaa thx for the follow-up and the alternate solution ! +1 – jbl Sep 23 '13 at 11:39