0

I have the following XML class structure:

[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] public class Envelope { [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] public Body Body { get; set; } } [XmlRoot(ElementName = "Body")] public class Body { [XmlElement(ElementName = "CustomerBundleMaintainConfirmation_sync_V1", Namespace = "http://sap.com/xi/SAPGlobal20/Global")] public CustomerMaintainConfirmation CustomerMaintainConfirmation { get; set; } } [XmlRoot(ElementName = "CustomerBundleMaintainConfirmation_sync_V1")] public class CustomerMaintainConfirmation { [XmlElement(ElementName = "Log")] public Log Log { get; set; } } [XmlRoot(ElementName = "Log")] public class Log { [XmlElement(ElementName = "MaximumLogItemSeverityCode")] public string MaximumLogItemSeverityCode { get; set; } [XmlElement(ElementName = "Item")] public Item Item { get; set; } } [XmlRoot(ElementName = "Item")] public class Item { [XmlElement(ElementName = "TypeID")] public string TypeID { get; set; } [XmlElement(ElementName = "CategoryCode")] public string CategoryCode { get; set; } [XmlElement(ElementName = "SeverityCode")] public string SeverityCode { get; set; } [XmlElement(ElementName = "Note")] public string Note { get; set; } } 

And this is the XML I am working with:

<?xml version="1.0" encoding="UTF-8"?> <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:Header /> <soap-env:Body> <n0:CustomerBundleMaintainConfirmation_sync_V1 xmlns:n0="http://sap.com/xi/SAPGlobal20/Global" xmlns:prx="urn:sap.com:proxy:LBK:/1SAI/TAE6F3228CC6D723FF1823E:804"> <Log> <MaximumLogItemSeverityCode>3</MaximumLogItemSeverityCode> <Item> <TypeID>018</TypeID> <CategoryCode>YES</CategoryCode> <SeverityCode>0</SeverityCode> <Note>TestNotes</Note> </Item> </Log> </n0:CustomerBundleMaintainConfirmation_sync_V1> </soap-env:Body> </soap-env:Envelope> 

When I attempt to deserialize this data into my classes, for whatever reason the "Log" class is null. Envelope, Body and CustomerMaintainConfirmation are all populated correctly. I see no reason why this is the case, but I've been staring at this for so long as this point I'm absolutely sure I am missing a mistake in and amongst my code somewhere.

This last piece of code is inside of the method that does the actual deserializing:

XmlSerializer serializer = new XmlSerializer(typeof(Envelope)); using (StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return (Envelope)serializer.Deserialize(responseReader); } 

I'm still working through it, but if anyone could point out any issues they might see with what I've provided, please let me know.

2 Answers 2

2

It's a namespace inconsistency. It deserializes with this change:

[XmlRoot(ElementName = "CustomerBundleMaintainConfirmation_sync_V1")] public class CustomerMaintainConfirmation { [XmlElement(ElementName = "Log", Namespace = "")] public Log Log { get; set; } } 
Sign up to request clarification or add additional context in comments.

Comments

1

You just miss the empty namespace indication on your element:

[XmlRoot(ElementName = "CustomerBundleMaintainConfirmation_sync_V1")] public class CustomerMaintainConfirmation { [XmlElement(ElementName = "Log", Namespace = "")] public Log Log { get; set; } } 

This is required, as apparently, your Log element has the empty namespace even though the CustomerBundleMaintainConfirmation_sync_V1 above has a nonempty one.

That's the only change in your code to make it work as shown in a fiddle.

4 Comments

This is one of those bittersweet answers. I love how simple my mistake was, but hate myself for it. Thanks so much!
Do you have any insight on why Log requires a namespace to be deserialized properly? I have no idea.
Without an explicit namespace the Log node takes the namespace of its parent. You can easily verify that by creating a non empty object, serializing it and examining the output. This is a general approach to testing possible namespace issues.
That makes sense. Thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.