我正在使用 .NET XSD.EXE导入器从一组 XSD 文件中生成 C# 类。当我尝试将其中一个类序列化为 XML 时,它失败了(InvalidOperationException),当我深入研究它时,我发现其中一个创建的类似乎是错误的。
这是相关的 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 有问题?
我想要的是正确序列化为针对 XSD 进行验证的 XML 的 C# 代码。现在锯齿状数组似乎是错误的,因为如果我删除它,它就会生成 XML。
我正在使用 Visual Studio 2008。