我正在使用.NET导入器从一组XSD文件生成C#类。当我试图将其中一个类序列化为XML时,它失败了(InvalidOperationException),当我挖掘到它时,我发现其中一个创建的类似乎是错误的。.NET xsd导入器创建不可序列化的类
下面是相关的XSD代码:
<xsd:complexType name="SuccessType">
<xsd:annotation>
<xsd:documentation>Indicates in a response message that a request was successfully processed.</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="Warnings" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<!-- .. snip .. -->
<xsd:element name="Warnings" type="WarningsType">
<xsd:annotation>
<xsd:documentation>The processing status of a business message and any related warnings or informational messages.</xsd:documentation>
</xsd:annotation>
</xsd:element>
<!-- .. snip .. -->
<xsd:complexType name="WarningsType">
<xsd:annotation>
<xsd:documentation>A collection of warnings generated by the successful processing of a business message.</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="Warning" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<!-- .. snip .. -->
<xsd:element name="Warning" type="WarningType">
<xsd:annotation>
<xsd:documentation>Defines details of a warning that occurred during message processing.</xsd:documentation>
</xsd:annotation>
</xsd:element>
<!-- .. snip .. -->
<xsd:complexType name="WarningType">
<xsd:annotation>
<xsd:documentation>Defines details of a warning that occurred during message processing.</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="WarningCategory"/>
<xsd:element ref="WarningCode"/>
<xsd:element ref="WarningShortMessage"/>
<xsd:element ref="WarningMessage"/>
</xsd:sequence>
</xsd:complexType>
这里是从它产生的C#代码:
public partial class SuccessType
{
private WarningType[][] warningsField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("Warning", typeof(WarningType), IsNullable = false)]
public WarningType[][] Warnings
{
get
{
return this.warningsField;
}
set
{
this.warningsField = value;
}
}
}
它使Warnings
的WarningType
阵列的阵列。当我尝试序列化到XML,我得到一个InvalidOperationException
例外:
- 无法生成临时类(结果= 1)。
- 错误CS0030:无法将类型 'WarningType []' 到 'WarningType'
- 错误CS0030:无法将类型 'WarningType []' 到 'WarningType'
- 错误CS0029:无法隐式转换类型 'WarningType' 来'WarningType []'
- 错误CS0029:无法隐式转换类型 'WarningType' 到 'WarningType []'
但是,如果我生成的代码从WarningType[][]
到WarningType[]
更改然后将其序列罚款。
无论何时重新生成(希望将不再频繁前进)编辑生成的C#类的缺点,是否还有其他解决方案?这是xsd.exe中的错误还是XSD文件不正确?也许在XmlSerializer中有问题?
我想要的是C#代码,它能正确序列化为XML,并对XSD进行验证。现在锯齿阵列似乎是错误的,因为如果我删除它然后它会生成XML。
我正在使用Visual Studio 2008.
我相信这是一个已知的bug,不会被修复。请参阅https://connect.microsoft.com/VisualStudio/feedback/details/362727/xsd-exe-incorrect-class-generated-for-abstract-type-with-derived-types了解他们认为不会出现的另一个XSD错误固定。 – 2010-02-06 01:31:17
@约翰桑德斯 - 布吉尔,那就是我想的。您知道XSD.exe是否有很好的选择? – 2010-02-06 01:53:36
.NET 4.5.1中仍然存在此问题 – yW0K5o 2015-03-02 16:36:36