Hi,
This issue is related to System.Xml.Serialization.XmlElementArrayAttribute
class.
We are using VS.NET 2005 (RTM) Professional Edition version 8.0.50727.42
(RTM.050727-4200).
-----------------------------------------------------------------------
Consider the following XSD:
-----------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="result" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="choice" nillable="true">
<xs:complexType>
<xs:simpleContent msdata:ColumnName="choice_Text" msdata:Ordinal="0">
<xs:extension base="xs:string">
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="result" msdata:IsDataSet="true"
msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="choice" />
<xs:element name="video_template">
<xs:complexType>
<xs:sequence>
<xs:element name="laserdisk_stock" type="xs:string"
minOccurs="0" />
<xs:element name="user_rating" minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element ref="choice" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
-----------------------------------------------------------------------
Importing this XSD using XSD.EXE generates the following CS file:
(comments removed for brevity)
---------------------------------------------------------------------------
using System.Xml.Serialization;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="", IsNullable=true)]
public partial class choice {
[System.Xml.Serialization.XmlTextAttribute()]
public string Value;
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="", IsNullable=false)]
public partial class result {
[System.Xml.Serialization.XmlElementAttribute("choi ce", typeof(choice),
IsNullable=true)]
[System.Xml.Serialization.XmlElementAttribute("vide o_template",
typeof(resultVideo_template),
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public object[] Items;
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=true)]
public partial class resultVideo_template {
[System.Xml.Serialization.XmlElementAttribute(Form= System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string laserdisk_stock;
[System.Xml.Serialization.XmlArrayAttribute(Form=Sy stem.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("ch oice", typeof(choice))]
public choice[][] user_rating;
}
-----------------------------------------------------------------------
Trying to serialize an object of type "result" into an Xml file using the
XmlSerializer Class causes an exception to occur at the following code:
XmlSerializer xmlSer = new XmlSerializer(typeof(result));
-----------------------------------------------------------------------
Exception:
Error while creating XmlSerializer object.
Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'choice[]' to 'choice'
error CS0029: Cannot implicitly convert type 'choice' to 'choice[]'
-----------------------------------------------------------------------
Our Analysis:
-----------------------------------------------------------------------
When we try to serialize type result, the XmlSerializer gets
an ambiguous match between choice and resultVideo_template.user_rating while
creating the XmlSerializer object.
To disambiguate the XmlSerializer we have to correctly specify (since
user_rating field is of type choice[][]) that a single element of this type
is "choice[]" instead of "choice". This issue exists with all multi-
dimensional arrays.
This should have been correctly generated by XSD.EXE.
-----------------------------------------------------------------------
Thanks in advance.
Kapil
(Persistent Systems Pvt. Ltd.)