Failed to create temporary class (result = 1). error CS0030: Unable to convert type
Identical symptoms for this post
The main symptoms are: When I use xsd.exe to generate my C # class from my XML, when I try to deserialize it, I get this error (it compiles, but generates a runtime error).
Unable to create temporary class (result = 1). Error CS0030: Unable to convert type 'ProductEnvironmentServerRolesServerRole []' to 'ProductEnvironmentServerRolesServerRole'
Original (from .cs generated by (xsd.exe):
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("ServerRole", typeof(ProductEnvironmentServerRolesServerRole), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public ProductEnvironmentServerRolesServerRole[][] ServerRoles
{
get { return this.serverRolesField; }
set { this.serverRolesField = value; }
}
But following the advice of Ajax, I changed the following From line:
public ProductEnvironmentServerRolesServerRole[][] ServerRoles {
To:
public ProductEnvironmentServerRolesServerRole[] ServerRoles {
This results in the following compilation errors:
Error 1 Unable to implicitly convert type 'ProductEnvironmentServerRolesServerRole [] []' to 'ProductEnvironmentServerRolesServerRole []' d: \ Users ... \ Documents \ Visual Studio 2012 \ Projects \ ReadingXMLDummy \ ReadingXMLDummy \ ProductEnvironmentXMLDummy
error implicitly 2 type 'ProductEnvironmentServerRolesServerRole []' in 'ProductEnvironmentServerRolesServerRole [] []' d: \ Users ... \ Documents \ Visual Studio 2012 \ Projects \ ReadingXMLDummy \ ReadingXMLDummy \ ProductEnvironment.cs 43 37 ReadingXMLDummy
Both are inverse to each other. I am very confused and desperate for help :-) Thanks in advance
PasteBin: Program.cs PasteBin: ProductEnvironment.cs
source to share
The problem here is not that the type is generated for the property, but introduce an attribute for the property:
[System.Xml.Serialization.XmlArrayItemAttribute("ServerRole", typeof(ProductEnvironmentServerRolesServerRole), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
should be changed to
[System.Xml.Serialization.XmlArrayItemAttribute("ServerRole", typeof(ProductEnvironmentServerRolesServerRole[]), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
source to share
As a property and its support, the field will need to be changed. As ProductEnvironment.cs
you also need to change this line:
private ProductEnvironmentServerRolesServerRole[][] serverRolesField;
to read the following:
private ProductEnvironmentServerRolesServerRole[] serverRolesField;
The error messages are reversed of each other because the property is get
trying to return your field (jagged array) as a one-dimensional array, while it set
tries to assign a one-dimensional array to (jagged array). (Line 40 is get, line 43 is set.)
You might want to apply the same to a property Servers
.
I'm not sure what would make XSD.exe generate such awful code, though ...
source to share