Microsoft KB Archive/319922

= BUG: XML serialization incorrectly processes MaxOccurs=&quot;0&quot; =

Article ID: 319922

Article Last Modified on 5/12/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.0
 * Microsoft ADO.NET 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 &quot;0&quot; 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=&quot;0&quot;. This means that the DataSet.ReadXmlSchema method does not ignore those elements.



CAUSE
If an element has maxOccurs=&quot;0&quot;, the correct interpretation from a relational view is &quot;this column does not exist&quot;. 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.



Steps to Reproduce the Problem
  Copy and paste the following XSD code into a text file, and then save this text file as c:\Test.xsd:          </li> <li>Run the following command from the &quot;Visual Studio .NET Command Prompt&quot; 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

</li> <li> View the Test.cs file that is generated: using System.Xml.Serialization;

/// [System.Xml.Serialization.XmlRootAttribute(&quot;top&quot;, Namespace=&quot;&quot;, IsNullable=true)] public class My { ///    public string str; }

</li> <li> The correct result should be: using System.Xml.Serialization;

/// [System.Xml.Serialization.XmlRootAttribute(&quot;top&quot;, Namespace=&quot;&quot;, IsNullable=true)] public class My { }                   </li></ol>

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=&quot;0&quot;: private DataColumn columnstr; ... this.columnstr = this.Columns[&quot;str&quot;]; ... this.Columns.Add(this.columnstr);

<div class="references_section">