当存在 xsd:choice 元素时,Linq2Xsd 只会在序列上跳闸。
幸运的是,我能够删除我正在使用的Amazon XSD的 xsd:choice (我只是没有使用 MerchantOrderID),这使得序列可以正确地保存在ToString()
xml 中。
<xsd:choice> <--- removed line
<xsd:element ref="AmazonOrderID"/>
<xsd:element ref="MerchantOrderID"/> <--- removed line
</xsd:choice> <--- removed line
<xsd:element name="ActionType" minOccurs="0" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Refund"/>
<xsd:enumeration value="Cancel"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
然后生成的代码在构造函数中正确地具有 this 保留顺序
contentModel = new SequenceContentModelEntity(
new NamedContentModelEntity(XName.Get("AmazonOrderID", "")),
new NamedContentModelEntity(XName.Get("ActionType", "")),
new NamedContentModelEntity(XName.Get("CODCollectionMethod", "")),
new NamedContentModelEntity(XName.Get("AdjustedItem", "")));
您也可以通过自己对其进行子类化来手动执行此操作,但我不确定这将如何与 xsd:choice 一起使用。这在此处进行了描述,但我尚未对其进行测试。