.NET xsd importer creates a class of a non-realizable class

I am using the .NET XSD.EXE importer to create C # classes from a collection of XSD files. When I tried to serialize one of the classes in XML, it failed ( InvalidOperationException ), and when I broke into it, I found that one of the created classes seems to be wrong.

Here is the corresponding XSD code:

<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> 

And here is the C # code generated from it:

 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; } } } 

He made the Warnings array of the WarningType array. When I try to serialize this to XML, I get an InvalidOperationException :

  • It is not possible to create a temporary class (result = 1).
  • error CS0030: cannot convert type 'WarningType []' to 'WarningType'
  • error CS0030: cannot convert type 'WarningType []' to 'WarningType'
  • error CS0029: Unable to implicitly convert type 'WarningType' to 'WarningType []'
  • error CS0029: Unable to implicitly convert type 'WarningType' to 'WarningType []'

But if I generated the generated code from WarningType[][] to WarningType[] , then it serializes.

With the exception of editing the generated C # class whenever I regenerate it (which I hope will be less likely to go forward), is there another solution? Is this an error in the xsd.exe file or an invalid XSD file? Maybe the problem is in the XmlSerializer?

What I want is C # code that serializes correctly in XML that validates XSD. The kernel array seems to be wrong right now, because if I delete it, it generates XML.

I am using Visual Studio 2008.

+6
c # xml xsd invalidoperationexception
source share
1 answer

There are errors in the xsd.exe tool. I don’t remember this particular one, but I remember that I find problems with gear arrays, and this is possible, this is a mistake. if you want, you can use the XsdObjbectGen tool, also from Microsoft, but released independently and out of range of .NET. SDK

One thing you can do is go backwards: write C # code, and then generate the circuit using xsd.exe and see what is different. Perhaps xsd.exe wants the circuit to look a certain way in order to correctly generate the correct code for the jagged arrays.


Actually, when re-reading your question, you never said what you really wanted. Do you want SuccessType to contain an array of arrays or not?

And is it WarningsType or WarningType ? There are some differences between the codes you provided.


Assuming you need an array of arrays, I wrote this C # code:

 public class WarningType { public String oof; } 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; } } } 

... then compiled it into a DLL. Then I ran xsd.exe in this DLL and generated this XSD:

 <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="WarningType" nillable="true" type="WarningType" /> <xs:complexType name="WarningType"> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" name="oof" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:element name="SuccessType" nillable="true" type="SuccessType" /> <xs:complexType name="SuccessType"> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" name="Warnings" type="ArrayOfArrayOfWarningType" /> </xs:sequence> </xs:complexType> <xs:complexType name="ArrayOfArrayOfWarningType"> <xs:sequence> <xs:element minOccurs="0" maxOccurs="unbounded" name="Warning" type="ArrayOfWarningType" /> </xs:sequence> </xs:complexType> <xs:complexType name="ArrayOfWarningType"> <xs:sequence> <xs:element minOccurs="0" maxOccurs="unbounded" name="WarningType" nillable="true" type="WarningType" /> </xs:sequence> </xs:complexType> </xs:schema> 

... and these are circular trips. If I run xsd.exe in this circuit, I get a type that wraps an array of arrays.

+4
source share

All Articles