.NET XSD.EXEインポーターを使用して、XSDファイルのコレクションからC#クラスを生成しています。クラスの1つをXMLにシリアル化しようとすると失敗し(InvalidOperationException)、それを掘り下げたときに、作成されたクラスの1つが間違っているように見えることがわかりました。
関連する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例外が発生します。
- 一時クラスを生成できません(result = 1)。
- エラーCS0030:タイプ'WarningType[]'を'WarningType'に変換できません
- エラーCS0030:タイプ'WarningType[]'を'WarningType'に変換できません
- エラーCS0029:タイプ'WarningType'を'WarningType[]'に暗黙的に変換できません
- エラーCS0029:タイプ'WarningType'を'WarningType[]'に暗黙的に変換できません
しかし、生成されたコードをからに変更すると、WarningType[][]正常WarningType[]にシリアル化されます。
これを再生成するたびに生成されたC#クラスを編集する以外に(今後は頻度が少なくなることを願っています)、他の解決策はありますか?これはxsd.exeのバグですか、それともXSDファイルが正しくありませんか?XmlSerializerに問題があるのではないでしょうか。
私が欲しいのは、XSDに対して検証するXMLに正しくシリアル化するC#コードです。今のところ、ジャグ配列は間違っているようです。削除するとXMLが生成されるからです。
VisualStudio2008を使用しています。