BUG: XML serialization incorrectly processes MaxOccurs="0" (319922)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework) 1.0
  • Microsoft ADO.NET (included with the .NET Framework 1.1)
  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft .NET Framework Class Libraries 1.1

This article was previously published under Q319922

SYMPTOMS

You can use the XML Schema Definition Tool (Xsd.exe) to generate the common language runtime classes from an XML schema. When you do so, elements that have the maxOccurs attribute with a value of "0" is supposed to be ignored. However, they may be included in the classes that are generated. When you generate the corresponding DataSet classes, the element is generated as a column.

The DataSet.ReadXmlSchema method also incorrectly interprets the XML schema when the schema contains elements that have maxOccurs="0". This means that the DataSet.ReadXmlSchema method does not ignore those elements.

CAUSE

If an element has maxOccurs="0", the correct interpretation from a relational view is "this column does not exist". But the element is not ignored and included in the runtime class that is generated or the DataSet class.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Problem

  1. Copy and paste the following XSD code into a text file, and then save this text file as c:\Test.xsd:
    <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="top" type="My" nillable="true"/>
      <xs:complexType name="My">
        <xs:sequence>
          <xs:element name="str" type="xs:string" maxOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="missme" type="xs:string" use="prohibited"/>
      </xs:complexType>
    </xs:schema>
    					
  2. Run the following command from the "Visual Studio .NET Command Prompt" window. This command generates a C# runtime class file that is named Test.cs in the current folder. Note that this is the folder from which you are running the Xsd.exe tool at the command prompt.

    xsd /c c:\test.xsd

  3. View the Test.cs file that is generated:
    using System.Xml.Serialization;
    
    /// <remarks/>
    [System.Xml.Serialization.XmlRootAttribute("top", Namespace="", IsNullable=true)]
    public class My {
        
        /// <remarks/>
        public string str;
    }
    
    					
  4. The correct result should be:
    using System.Xml.Serialization;
    
    /// <remarks/>
    [System.Xml.Serialization.XmlRootAttribute("top", Namespace="", IsNullable=true)]
    public class My {
        }
    					
NOTE: You can also use the xsd /d c:\test.xsd command to create the corresponding DataSet class. The Test.cs runtime class file that is generated contains the following code excerpts that shows a DataColumn is being created for the element with maxOccurs="0":
private DataColumn columnstr;
...
this.columnstr = this.Columns["str"]; 
...
this.Columns.Add(this.columnstr);
				

REFERENCES

For more information about the XML Schema Definition Tool and XML Serialization, visit the following Microsoft Web sites: For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

314150 INFO: Roadmap for XML Serialization in the .NET Framework


Modification Type:MinorLast Reviewed:3/9/2006
Keywords:kbtshoot kbbug kbenv kbpending KB319922 kbAudDeveloper