Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# deserializing xml with multiple possible namespaces

I created an API wrapper class library for consuming a rest API from a 3rd party.

It was all working until they recently updated the API in the latest version of their product and added a namespace to the root element, now my deserialization code is failing.

An example of one of my classes:

[Serializable]
[XmlRootAttribute(ElementName = "exit_survey_list")]
public class SupportExitSurveyCollection : ApiResult { .... }

If I set the Namespace property in the XmlRootAttribute to the new namespace being returned, then it works properly again.

But I need to support both versions of the API (namespaced and not) because I cannot be sure which version of the API will be available.

I'd like to get this working without duplicating classes for different versions, but not sure if it's possible.

Thanks for any input/advice.

like image 779
Adam Avatar asked Feb 11 '11 17:02

Adam


1 Answers

I don't think that is possible.

You could implement the IXmlSerializable interface, and control serialization yourself - that would work but it is probably not what you want, since it would require you to do a lot of the mapping yourself in code.

Another option would be to pre-process the messages and add the namespace if it is missing. Then you can have a single deserialization process.

like image 85
driis Avatar answered Nov 14 '22 22:11

driis