locked
XSD Seems Not to Generate C# Code Properly.

    Question

  • The reason I posted this is to find out if this is an known issue with the XSD tool because working as a compiler to parse the XSD file, it should generate the following code

     

    titleType [] titleField;

    descriptionType [] DescriptionType

     

    But instead it generated

     

    langstring[][] title

     

    because each title has the langstring inside it but it should not make the assumption that title is langstring[][] because langstring is the only one element inside the title now what if tomorrow someone adds another field.

     

    The same thing happens when generating the dataset from the XSD

    it treats the title as an element in the langstring array

    langstring[1].name = "title" when dump it from the VS 2005 debugger.

     

    the XMLSerializer reports the following error when try to generate the XML output

     

    serializer.Serialize(typeof(generalType)

     

    Can't create temporary class 1, can't assign langstring tyo langstring[] 

     

    If I made the change to eliminate the maxOccur="Unbound" it generates

    titleType titleField as expected.

     

     

    Attached is the IMSGlobal Standard XSD imsmd_v1p2p4.xsd (http://www.imsglobal.org/xsd/imsmd_v1p2) The datatype generalType has the following definition

    <xsd:complexType name = "generalType" mixed = "true">
      <xsdTongue Tiedequence>
       <xsd:element ref = "identifier" minOccurs = "0"/>
       <xsd:element ref = "title" minOccurs = "0"/>
       <xsd:element ref = "catalogentry" minOccurs = "0" maxOccurs = "unbounded"/>
       <xsd:element ref = "language" minOccurs = "0" maxOccurs = "unbounded"/>
       <xsd:element ref = "description" minOccurs = "0" maxOccurs = "unbounded"/>
       <xsd:element ref = "keyword" minOccurs = "0" maxOccurs = "unbounded"/>
       <xsd:element ref = "coverage" minOccurs = "0" maxOccurs = "unbounded"/>
       <xsd:element ref = "structure" minOccurs = "0"/>
       <xsd:element ref = "aggregationlevel" minOccurs = "0"/>
       <xsd:group ref = "grp.any"/>
      </xsdTongue Tiedequence>
     </xsd:complexType>

     

    each of the element is defined in the element section. the definition is defined as definitionType,

    However, when running xsd.exe /c /nologo imsmd_v1p2p4.xsd

    the C# code for the generalType is

    public partial class generalType {
       
        private string identifierField;
       
        private langstringType[] titleField;
       
        private catalogentryType[] catalogentryField;
       
        private string[] languageField;
       
        private langstringType[][] descriptionField;
       
        private langstringType[][] keywordField;
       
        private langstringType[][] coverageField;
       
        private structureType structureField;
       
        private aggregationlevelType aggregationlevelField;
       
        private System.Xml.XmlNode[] anyField;
       

     

    the description is no longer the description type,

     

    I remove the maxOccur=unbounds and then the description is generate as descriptionType.

     

    The way the xsd is doing now to treat the description as langstring[][] will cause the XML serializer error when serialing it to an XML output.

     

    Anyone know why the XSD is behaving like this -- instead of the descriptionType, it generates langstring[][] and when the maxOccur is removed, the descriptionType is show up.

     

    You can try to run the XSD on the file after you get the file from the web site.

     

    Thank you

     

    Friday, April 06, 2007 10:44 PM

Answers

  • Hello,

     

    Yes, you are running into a known issue with the XmlSerializer when it tries to generate arrays of arrays.

     

    To work around this issue, you can wrap the inner schema constructs in a benign construct such as an xsTongue Tiedequence, like this:

     

    <xsd:complexType name="keywordType">
        <!-- Work arround array of arrays issue -->

      <xsdTongue Tiedequence>
          <xsdTongue Tiedequence>
            <xsd:element ref="langstring" maxOccurs="unbounded" />
          </xsdTongue Tiedequence>
        </xsdTongue Tiedequence>
      </xsd:complexType>

     

    This will cause the XmlSerializer to generate separate types instead of arrays of arrays.

     

    Daniel Roth

    Thursday, April 12, 2007 8:11 PM
    Owner