Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top