MrFiveT
MrFiveT

Reputation: 741

Unable to Deserialize XML within C#

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!

Upvotes: 4

Views: 746

Answers (2)

Charles Mager
Charles Mager

Reputation: 26233

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

Upvotes: 4

Scoregraphic
Scoregraphic

Reputation: 7210

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.

Upvotes: 1

Related Questions