C# 使用多个可能的命名空间反序列化 xml
我创建了一个 API 包装器类库,用于使用来自第 3 方的 REST API。
一切都正常,直到他们最近在其产品的最新版本中更新了 API 并向根元素添加了命名空间,现在我的反序列化代码失败了。
我的类之一的示例:
[Serializable]
[XmlRootAttribute(ElementName = "exit_survey_list")]
public class SupportExitSurveyCollection : ApiResult { .... }
如果我将 XmlRootAttribute 中的 Namespace 属性设置为返回的新命名空间,那么它会再次正常工作。
但我需要支持两个版本的 API(命名空间和非命名空间),因为我无法确定哪个版本的 API 可用。
我希望在不为不同版本重复类的情况下完成此工作,但不确定是否可能。
感谢您的任何意见/建议。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是不可能的。
您可以实现 IXmlSerialized 接口,并控制自己序列化 - 这可以工作,但可能不是您想要的,因为它需要您自己在代码中进行大量映射。
另一种选择是预处理消息并添加名称空间(如果缺少)。然后你就可以有一个反序列化过程。
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.