简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM