如何处理基于XML的协议,其中响应可能符合两个XSD之一?
我必须通过协议读取和写入数据,其中响应XML可能会根据服务器应用程序的错误状态不同。如果响应很好,它会使用特定模式使用XML_1,但是如果响应表示错误,则使用具有完整不同架构的XML_2。我认为的好设计是将错误状态纳入第一个架构,但是我们只是该服务的消费者,我们无法访问服务器应用程序的设计。我的解决方案是(使用C#)将XML响应读为字符串,进行一些搜索,以了解正在使用哪个XML模式,然后使用适当的XML Serialializer将响应转换为对象。有一个更优雅的解决方案吗?
I have to read and write data through a protocol where the response XML maybe different according to the error state of the server application. If the response is good it uses let's say Xml_1 with a specific schema but if the response indicates an error it uses Xml_2 with a complete different schema. The good design , in my opinion would be to incorporate the error state to the first schema, but we are just consumers of the this service and we don't have access to the design of the server application. My solution is to (using C#) read the XML response as string, do some searching in order to understand which XML schema is in use and then using the appropriate XML Serializer to convert the response to an object. Is there a more elegant solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

两个模式的结合是有效的模式吗? (例如,如果它们使用不同的名称空间,则通常是这种情况,但是如果它们都是无名空间模式,或者它们是同一模式的两个版本)。
如果工会是有效的模式,那么您可以考虑对此进行阀门。
否则,窥视文件开始时通常足以告诉您使用哪种词汇。
可以在未经验证的情况下解析XML文档,对其进行检查,然后验证已经解析的文档。甚至可以在单个管道中执行此操作,而不会将整个文档放入内存中。但是详细信息取决于您使用的工具包。您已经标记了问题C# - 我不确定使用Microsoft工具是否可以使用,但是我认为可能使用Saxon -CS。 [免责声明,我的产品]。
Is the union of the two schemas a valid schema? (This will typically be the case, for example, if they use different namespaces, but it's likely not to be the case if they are both no-namespace schemas or if they are two versions of the same schema).
If the union is a valid schema, then you could consider validing against that.
Otherwise peeking at the start of the file will often be enough to tell you which vocabulary is in use.
It's possible to parse an XML document without validation, inspect it, and then validate the already parsed document. It's even possible to do this in a single pipeline without putting the whole document in memory. But the details depend on the toolkit you are using. You've tagged the question C# - I'm not sure if this is possible using the Microsoft tools, but it should be possible I think using Saxon-CS. [Disclaimer, my product].