xsd.exe generates code with the wrong type in an XmlArrayItemAttribute - by Andrew Paprocki

Status : 

  Won't Fix<br /><br />
		Due to several factors the product team decided to focus its efforts on other items.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


4
0
Sign in
to vote
ID 349967 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 6/8/2008 9:01:58 PM
Access Restriction Public

Description

I am attaching a very simple XSD schema. The generated C# code when using xsd.exe Version=2.0.50727.1432 contains the wrong type in one of the XmlArrayItemAttribute attributes.

This schema:
<xs:schema id="Test.Items" version="1.0"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        attributeFormDefault="unqualified" elementFormDefault="qualified">
        <xs:element name="top" type="Items"/>
        <xs:complexType name="Items">
                <xs:sequence>
                        <xs:element name="items" type="Item" minOccurs='0' maxOccurs='unbounded' />
                </xs:sequence>
        </xs:complexType>
        <xs:complexType name="Item">
                <xs:sequence>
                        <xs:element name="item" type="xs:string" minOccurs='1' maxOccurs='unbounded' />
                </xs:sequence>
        </xs:complexType>
</xs:schema>

Generates this broken code:
    [System.Xml.Serialization.XmlArrayItemAttribute("item", typeof(string), IsNullable=false)]
    public string[][] items {

The typeof(string) should be a typeof(string[]). When using the generated code, the application fails at runtime:

Unhandled Exception: System.InvalidOperationException: Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'string[]' to 'string'
error CS0029: Cannot implicitly convert type 'string' to 'string[]'

   at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
   at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
   at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
   at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace)
   at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
   at System.Xml.Serialization.XmlSerializer..ctor(Type type)
   at Items.App.Main(String[] args)
Sign in to post a comment.
Posted by Microsoft on 8/30/2008 at 12:14 PM
This is a bug in how xsd.exe handles multi-dimentional arrays. To avoid the risk of regressing the functionality of xsd.exe, we do not plan on fixing this issue at this time. Please use the following workaround:

Add an optional attribute to the Item type, like this:
<xs:complexType name="Item">
    <xs:sequence>
     <xs:element name="item" type="xs:string" minOccurs='1' maxOccurs='unbounded' />
    </xs:sequence>
    <xs:attribute name="OptionalAttribute" type="xs:string" />
</xs:complexType>

Rerun xsd.exe on the modified schema. The application will now successfully deserialize the XML file.

Daniel Roth
Program Manager
Posted by Microsoft on 6/10/2008 at 5:38 PM
Thanks for your feedback. We are escalating this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Thank you,
Visual Studio Product Team