In the xsd file there are complex types. Here is a
fragment:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="Information">
<xs:complexType>
<xs:sequence>
<xs:element name="CoIDs"
type="CoIDsType" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="CoIDsTypeRestriction">
<xs:restriction base="xs:string">
<xs:minLength value="1" />
<xs:maxLength value="50" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="CoIDType">
<xs:simpleContent>
<xs:extension
base="CoIDsTypeRestriction">
<xs:attribute name="Type"
use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="4" />
<xs:maxLength value="11" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="CoIDsType">
<xs:sequence>
<xs:element name="CoID"
type="CoIDType" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>
The xsd file validates 60,000 files without any problems.
I use the following to generate the class file:
Xsd -c trycomplextypes.xsd -e:Information -
n:trycomplextypes
Also
Xsd -c trycomplextypes.xsd -n:trycomplextypes
I am trying to use XmlSerializer. Here is a code fragment
where it fails:
XmlSerializer serializer = new XmlSerializer(typeof
(Information));
In both cases I get the following error message:
An unhandled exception of
type 'System.InvalidOperationException' occurred in
system.xml.dll
Additional information: Unable to generate a temporary
class (result=1).
error CS0030: Cannot convert type 'trycomplextypes.CoIDType
[]' to 'trycomplextypes.CoIDType'
error CS0030: Cannot convert type 'trycomplextypes.CoIDType
[]' to 'trycomplextypes.CoIDType'
error CS0029: Cannot implicitly convert
type 'trycomplextypes.CoIDType'
to 'trycomplextypes.CoIDType[]'
error CS0029: Cannot implicitly convert
type 'trycomplextypes.CoIDType'
to 'trycomplextypes.CoIDType[]'
The program '[3108] trycomplextypes.exe' has exited with
code 0 (0x0).
Is there an issue or resolution to this problem?
Thank you,
John Pearson
What you have run into is a known problem with the XmlSerializer when
generating classes from XML schemas whose members need to be represented as
jagged arrays. On running XSD against your schema, the following is the
class generated for the Infomation type:
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public class Information {
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("CoID",
typeof(CoIDType), IsNullable=false)]
public CoIDType[][] CoIDs;
}
If you notice carefully, you will see that the public CoIDs field is a
jagged array. In a jagged array, each element of the array is an array by
itself. This needs to be reflected in the type specified in the
XmlArrayItemAttribute that describes how the field should be serialized.
You will need to manually change the above definition to the following to
reflect the array type of each element of the jagged array and to get your
code to run (Notice the change in the type specified in the
XmlArrayItemAttribute):
System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public class Information {
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("CoID",
typeof(CoIDType[]), IsNullable=false)]
public CoIDType[][] CoIDs;
}
While your code will now execute and you will be able to serialize your
object instances to XML, you may have some problems when attempting to
se-serialize the data. This can happen in a few cases and is again a known
behavior. Implement this change and test the serialization/de-serialization
to see how it works in your case.
Thanks
Karthik
This posting is provided "AS IS" with no warranties and confers no rights.
I altered the code to support the jagged array and de-
serialized the xml file.
File size is 526KB and time to de-serialize between 312 to
657 Milliseconds.
Thank you,
John Pearson