Hi i am getting the following error while using XMLSerializer
XmlSerializer ser = new XmlSerializer(t ypeof(Person));
ERROR:
Unable to generate a temporary class (result=1).
error CS0266: Cannot implicitly convert type 'XSD.BiodataGen der?' to
'XSD.BiodataRac e?'. An explicit conversion exists (are you missing a cast?)
I am getting the above error when my xsd has multiple nillable elements of
userdefined type. If I change the type of either race or gender to String i
am getting any error. temporary class is generated easily
MY XSD :
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema targetNamespace ="http://my/ex" xmlns:version1= "http://my/ex"
xmlns:xsd="http ://www.w3.org/2001/XMLSchema">
<xsd:element name="person" type="version1: Person" />
<xsd:complexTyp e name="Person">
<xsd:sequence >
<xsd:element name="biodata" type="version1: Biodata" minOccurs="1"
maxOccurs="1" />
</xsd:sequence>
</xsd:complexType >
<xsd:complexTyp e name="Biodata">
<xsd:sequence >
<xsd:element name="gender" nillable="true" >
<xsd:simpleType >
<xsd:restrictio n base="xsd:strin g">
<xsd:enumeratio n value="M" />
<xsd:enumeratio n value="F" />
</xsd:restriction >
</xsd:simpleType>
</xsd:element>
<xsd:element name="race" nillable="true" >
<xsd:simpleType >
<xsd:restrictio n base="xsd:strin g">
<xsd:enumeratio n value="AI" />
<xsd:enumeratio n value="AS" />
</xsd:restriction >
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType >
</xsd:schema>
Code Generated by xsd.exe:
namespace XSD
{
/// <remarks/>
[System.CodeDom. Compiler.Genera tedCodeAttribut e("cxsc", "0.57.0.0")]
[System.Serializ ableAttribute()]
[System.Diagnost ics.DebuggerSte pThroughAttribu te()]
[System.Componen tModel.Designer CategoryAttribu te("code")]
[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://my/ex")]
[System.Xml.Seri alization.XmlRo otAttribute("pe rson",
Namespace="http ://my/ex", IsNullable=fals e)]
public partial class Person
{
/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( Form=System.Xml .Schema.XmlSche maForm.Unqualif ied)]
public Biodata biodata;
}
/// <remarks/>
[System.CodeDom. Compiler.Genera tedCodeAttribut e("cxsc", "0.57.0.0")]
[System.Serializ ableAttribute()]
[System.Diagnost ics.DebuggerSte pThroughAttribu te()]
[System.Componen tModel.Designer CategoryAttribu te("code")]
[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://my/ex")]
public partial class Biodata
{
/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( Form=System.Xml .Schema.XmlSche maForm.Unqualif ied, IsNullable=true )]
public System.Nullable <BiodataGenderg ender;
/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( Form=System.Xml .Schema.XmlSche maForm.Unqualif ied, IsNullable=true )]
public System.Nullable <BiodataRacerac e;
}
/// <remarks/>
[System.CodeDom. Compiler.Genera tedCodeAttribut e("cxsc", "0.57.0.0")]
[System.Serializ ableAttribute()]
[System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
Namespace="http ://my/ex")]
public enum BiodataGender
{
/// <remarks/>
M,
/// <remarks/>
F,
}
/// <remarks/>
[System.CodeDom. Compiler.Genera tedCodeAttribut e("cxsc", "0.57.0.0")]
[System.Serializ ableAttribute()]
[System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
Namespace="http ://my/ex")]
public enum BiodataRace
{
/// <remarks/>
AI,
/// <remarks/>
AS,
}
}
XmlSerializer ser = new XmlSerializer(t ypeof(Person));
ERROR:
Unable to generate a temporary class (result=1).
error CS0266: Cannot implicitly convert type 'XSD.BiodataGen der?' to
'XSD.BiodataRac e?'. An explicit conversion exists (are you missing a cast?)
I am getting the above error when my xsd has multiple nillable elements of
userdefined type. If I change the type of either race or gender to String i
am getting any error. temporary class is generated easily
MY XSD :
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema targetNamespace ="http://my/ex" xmlns:version1= "http://my/ex"
xmlns:xsd="http ://www.w3.org/2001/XMLSchema">
<xsd:element name="person" type="version1: Person" />
<xsd:complexTyp e name="Person">
<xsd:sequence >
<xsd:element name="biodata" type="version1: Biodata" minOccurs="1"
maxOccurs="1" />
</xsd:sequence>
</xsd:complexType >
<xsd:complexTyp e name="Biodata">
<xsd:sequence >
<xsd:element name="gender" nillable="true" >
<xsd:simpleType >
<xsd:restrictio n base="xsd:strin g">
<xsd:enumeratio n value="M" />
<xsd:enumeratio n value="F" />
</xsd:restriction >
</xsd:simpleType>
</xsd:element>
<xsd:element name="race" nillable="true" >
<xsd:simpleType >
<xsd:restrictio n base="xsd:strin g">
<xsd:enumeratio n value="AI" />
<xsd:enumeratio n value="AS" />
</xsd:restriction >
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType >
</xsd:schema>
Code Generated by xsd.exe:
namespace XSD
{
/// <remarks/>
[System.CodeDom. Compiler.Genera tedCodeAttribut e("cxsc", "0.57.0.0")]
[System.Serializ ableAttribute()]
[System.Diagnost ics.DebuggerSte pThroughAttribu te()]
[System.Componen tModel.Designer CategoryAttribu te("code")]
[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://my/ex")]
[System.Xml.Seri alization.XmlRo otAttribute("pe rson",
Namespace="http ://my/ex", IsNullable=fals e)]
public partial class Person
{
/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( Form=System.Xml .Schema.XmlSche maForm.Unqualif ied)]
public Biodata biodata;
}
/// <remarks/>
[System.CodeDom. Compiler.Genera tedCodeAttribut e("cxsc", "0.57.0.0")]
[System.Serializ ableAttribute()]
[System.Diagnost ics.DebuggerSte pThroughAttribu te()]
[System.Componen tModel.Designer CategoryAttribu te("code")]
[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://my/ex")]
public partial class Biodata
{
/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( Form=System.Xml .Schema.XmlSche maForm.Unqualif ied, IsNullable=true )]
public System.Nullable <BiodataGenderg ender;
/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( Form=System.Xml .Schema.XmlSche maForm.Unqualif ied, IsNullable=true )]
public System.Nullable <BiodataRacerac e;
}
/// <remarks/>
[System.CodeDom. Compiler.Genera tedCodeAttribut e("cxsc", "0.57.0.0")]
[System.Serializ ableAttribute()]
[System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
Namespace="http ://my/ex")]
public enum BiodataGender
{
/// <remarks/>
M,
/// <remarks/>
F,
}
/// <remarks/>
[System.CodeDom. Compiler.Genera tedCodeAttribut e("cxsc", "0.57.0.0")]
[System.Serializ ableAttribute()]
[System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
Namespace="http ://my/ex")]
public enum BiodataRace
{
/// <remarks/>
AI,
/// <remarks/>
AS,
}
}