简体   繁体   English

XSD向后兼容性

[英]XSD Backward compatibility

I have a XSD file with a schema that is used to define an XML interface for sending values into my application from remote clients. 我有一个带有架构的XSD文件,该架构用于定义一个XML接口,用于从远程客户端向我的应用程序发送值。 Right now, the XSD have, lets say for simplicity 3 elements. 现在,XSD拥有3个元素。

So in order to serialize and deserialize this in C# I can use the tool xsd.exe to generate C# classes and use those classes to serialize and deserialize XML with respect to the XSD. 因此,为了在C#中对此序列化和反序列化,我可以使用工具xsd.exe生成C#类,并使用这些类对XSD进行XML序列化和反序列化。

This works, but then lets say I create a new version of the XSD, with a new fourth element to enable some server-side feature available if this element is specified for new remote clients, but I still want to support the old XSD (dont want or can change the software of the old remote clients), but the new classes generated from the new XSD wont be compatible with the old XMLs from the old XSD. 这可行,但是可以说我创建了XSD的新版本,并使用了第四个新元素来启用一些服务器端功能(如果为新的远程客户端指定了此元素,但是我仍然想支持旧的XSD(不要需要或可以更改旧的远程客户端的软件),但是从新的XSD生成的新类将与旧的XSD的旧XML不兼容。

I suppose I could read the XML's directly with XDocument or similar, but I want to have the feature of only accepting XML's that can be validated against one of the XSD (and then take server-side decision depending on which XSD version it validates against). 我想我可以直接用XDocument或类似的文件读取XML,但是我想具有仅接受可以针对其中一个XSD进行验证的XML(然后根据验证的XSD版本做出服务器端决定)的功能。 。 This is due to customer relation issues. 这是由于客户关系问题。

What is best practice to deal with this issues? 解决此问题的最佳实践是什么?

If I understand you correctly then you shouldn't need to support multiple XSD's if all you are trying to do is add an additional element to the schema. 如果我对您的理解正确,那么如果您想要做的就是向架构中添加其他元素,则无需支持多个XSD。 Instead, if you set the 'minOccurs' value to 0 for this new element and regenerate the CS file and use this to de-serialize your objects this should allow you to de-serialize requests for when the element is present and when it is not present. 相反,如果您将此新元素的'minOccurs'值设置为0并重新生成CS文件,然后使用它反序列化对象,则应该允许您反序列化元素存在和不存在的请求当下。 You can then just evaluate if the Property on the de-serialized object is null to determine whether the new feature should be enabled or not. 然后,您可以评估反序列化对象上的Property是否为空,以确定是否应启用新功能。 See example XSD below: 请参阅下面的示例XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="ClassName" nillable="true" type="ClassName" />
  <xs:complexType name="ClassName">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Property1" type="Property1Type" />
      <xs:element minOccurs="0" maxOccurs="1" name="Property2" type="Property2Type" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Best practice is to avoid data binding technology if your schema is likely to change, especially if you need to handle multiple variants. 最佳实践是在架构可能发生更改时避免使用数据绑定技术,尤其是在需要处理多个变体的情况下。 Basically, XML is designed for flexibility and languages like C# aren't, so if you compile C# code to reflect a specific schema then you are locking yourself in. Data binding is great if nothing changes, but if you need to handle variety and change then either use a generic approach (such as DOM) or use XML-specific processing languages such as XSLT and XQuery. 基本上,XML是为灵活性而设计的,而C#之类的语言则不是,因此,如果您编译C#代码以反映特定的模式,那么您就将自己锁定在其中。如果什么都没有改变,但是如果您需要处理变化和变化,那么数据绑定就很棒。然后要么使用通用方法(例如DOM),要么使用XML特定的处理语言(例如XSLT和XQuery)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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