4

I have some problem when trying to deserialize the following XML:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Response xmlns=\"http://Microsoft.LobServices.Sap/2007/03/Rfc/\">
<E_ARR>
    <ITEM xmlns=\"http://Microsoft.LobServices.Sap/2007/03/Types/Rfc/\">
        <PROPA>00100000</PROPA>
        <PROPB>0815</PROPB>
    </ITEM>
    <ITEM xmlns=\"http://Microsoft.LobServices.Sap/2007/03/Types/Rfc/\">
        <PROPA>00100001</PROPA>
        <PROPB>0123</PROPB>
    </ITEM>
</E_ARR>
</Response>

Using the following lines of code:

var reader = new StringReader(XmlShownAbove);
var serializer = new XmlSerializer(typeof(Response));
var instance = (Response)serializer.Deserialize(reader);

And the following two models:

[XmlRoot("Response", Namespace="http://Microsoft.LobServices.Sap/2007/03/Rfc/")]
public class Response
{
    [XmlArray("E_ARR", Namespace="")]
    [XmlArrayItem(typeof(ITEM), ElementName = "ITEM", Namespace="http://Microsoft.LobServices.Sap/2007/03/Types/Rfc/")]
    public ITEM[] E_ARR{ get; set; }
}

public class ITEM
{
    [XmlElement(Namespace = "")]
    public string PROPA{ get; set; }

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

Unfortunately this code does not deserialize the E_ARR-Array correctly - it allways remains null and I can't figure out why. I guess it is something simple but I just failed to see it - thanks in advance!

MrFiveT
  • 741
  • 6
  • 18

2 Answers2

4

Your classes don't quite match your XML. Your E_ARR element inherits the default namespace of its parent, so the namespace is in fact http://Microsoft.LobServices.Sap/2007/03/Rfc/. The same applies to PROPA and PROPB. Simply adding the correct namespaces should solve your issue:

[XmlRoot("Response", Namespace="http://Microsoft.LobServices.Sap/2007/03/Rfc/")]
public class Response
{
    [XmlArray("E_ARR", Namespace = "http://Microsoft.LobServices.Sap/2007/03/Rfc/")]
    [XmlArrayItem(typeof(ITEM), ElementName = "ITEM", Namespace="http://Microsoft.LobServices.Sap/2007/03/Types/Rfc/")]
    public ITEM[] E_ARR{ get; set; }
}

public class ITEM
{
    [XmlElement(Namespace = "http://Microsoft.LobServices.Sap/2007/03/Types/Rfc/")]
    public string PROPA{ get; set; }

    [XmlElement(Namespace = "http://Microsoft.LobServices.Sap/2007/03/Types/Rfc/")]
    public string PROPB{ get; set; }
}

Alternatively, as pointed out by Chris in the comments, omitting the namespace has the same effect - the namespace of the parent is inherited;

[XmlRoot("Response", Namespace = "http://Microsoft.LobServices.Sap/2007/03/Rfc/")]
public class Response
{
    [XmlArray("E_ARR")]
    [XmlArrayItem(typeof (ITEM), ElementName = "ITEM", Namespace = "http://Microsoft.LobServices.Sap/2007/03/Types/Rfc/")]
    public ITEM[] E_ARR { get; set; }
}

public class ITEM
{
    [XmlElement]
    public string PROPA { get; set; }

    [XmlElement]
    public string PROPB { get; set; }
}
Charles Mager
  • 25,735
  • 2
  • 35
  • 45
  • 1
    I was just about to answer something similar. Removing the namespace entirely from the attributes (the empty ones, not the ones with valid values) will work as well and is probably easier and I suspect is the exact same thing. – Chris Jul 07 '15 at 11:32
  • I decided to use the second suggest solution which works perfectly. Thanks a lot! – MrFiveT Jul 07 '15 at 11:38
1

You can add

   <system.diagnostics>
      <switches>
         <add name="XmlSerialization.Compilation" value="1" />
      </switches>
   </system.diagnostics>

to your application config file which dumps the generated XML serialization code files to your temporary folder. With these files, you can debug the whole serialization process and step through the code.

Have you tried parsing the file after manually removing all namespace stuff? Namespaces are the most painful part of XML processing.

Scoregraphic
  • 7,110
  • 4
  • 42
  • 64
  • 2
    @Chris, In my opinion it's a helpful answer to the question because it could help the OP to find a solution for his problem. SO is not meant to always provide fully working solutions, but that's my personal opinion. – Scoregraphic Jul 07 '15 at 11:50
  • I'd say it was a helpful comment but not a helpful answer. SO is a great place for learning new things but it is a question and answer site. Answers should answer the question as asked. That is just my opinion too though. There may well be meta discussions on it but I don't have strong enough feelings on the subject to search for them (and I didn't downvote either in case you were curious). – Chris Jul 07 '15 at 11:56