Microsoft KB Archive/816226

= BUG: XSD.exe generates cases that allow for multiple entries for all members of choice even though only one member is unbounded =

Article ID: 816226

Article Last Modified on 9/15/2005

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



SYMPTOMS
In an XML Schema project, the XSD tool incorrectly interprets the maxOccurs attribute for all members of the choice element as unbounded if one of the following criteria is true:
 * If the maxOccurs attribute is set to a value that is greater than 1.
 * If the maxOccurs attribute is unbounded for at least one member of the choice element.

With the managed class that is generated by the XSD tool, multiple unbounded entries can be added for all the members of the choice element in the XML document.



WORKAROUND
There is no direct workaround for this problem. However, in some cases you can work around this problem by using two choice elements. The first choice element has members with the maxOccur attribute set to one, and the second choice element has members with a maxOccur attribute that is set to greater than one. The following schema code describes the changes that are required in choice elements of the schema that are used in the &quot;More Information&quot; section of this article:               </xs:choice> <xs:element name=&quot;CustName&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;CustID&quot; type=&quot;xs:string&quot; /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> <xs:key name=&quot;RootKey1&quot;> <xs:selector xpath=&quot;.//mstns:Customers&quot; /> <xs:field xpath=&quot;mstns:CustID&quot; /> </xs:key> </xs:element> </xs:schema>

<div class="status_section">

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

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> <li>Start Microsoft Visual Studio .NET.</li> <li>On the File menu, point to New, and then click Project.</li> <li>Under Project Types, click Visual Basic Projects, or Visual C# Projects, and then under Templates, click Console Application.</li> <li>In the Name text box, type MyTestConsoleApplication, and in the Location text box, type C:\ , and then click OK.</li> <li> Replace the existing code with the following code:

Visual C# .NET Code
using System; using System.Data; using System.Xml.Serialization; using System.IO; using System.Xml; using System.Xml.Schema;

namespace MyTestConsoleApplication {  class MyTestClass {     [STAThread] static void Main(string[] args) {        // Deserializing the data in XML Document to the object of type // of the class generated by xsd.exe for XML Schema Root myRootObj; FileStream fStream = new FileStream(@&quot;C:\MyTestConsoleApplication\customers.xml&quot;, FileMode.Open); XmlSerializer mySerializer = new XmlSerializer(typeof(Root)); myRootObj = (Root)mySerializer.Deserialize(fStream); mySerializer.Serialize(System.Console.OpenStandardOutput, myRootObj); fStream.Close; Console.ReadLine; }  } }

Visual Basic .NET Code
Imports System Imports System.Data Imports System.Xml.Serialization Imports System.IO Imports System.Xml Imports System.Xml.Schema

Module Module1

Sub Main ' Deserializing the data in XML Document to the object of type ' of the class generated by xsd.exe for XML Schema Dim myRootObj As Root Dim fStream As New FileStream(&quot;C:\MyTestConsoleApplication\customers.xml&quot;, FileMode.Open) Dim mySerializer As New XmlSerializer(GetType(Root)) myRootObj = mySerializer.Deserialize(fStream) mySerializer.Serialize(System.Console.OpenStandardOutput, myRootObj) fStream.Close Console.ReadLine End Sub

End Module </li> <li>On the File menu, point to New, and then click File.</li> <li>Under Categories, click General, and then under Templates, click XML Schema. Click Open.</li> <li> Click the XML tab. Replace the existing code with the following XML Schema code: <?xml version=&quot;1.0&quot; ?> <xs:schema id=&quot;Root&quot; targetNamespace=&quot;http://tempuri.org/customer.xsd&quot; xmlns:mstns=&quot;http://tempuri.org/customer.xsd&quot; xmlns=&quot;http://tempuri.org/customer.xsd&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:msdata=&quot;urn:schemas-microsoft-com:xml-msdata&quot; attributeFormDefault=&quot;qualified&quot; elementFormDefault=&quot;qualified&quot;> <xs:element name=&quot;Root&quot; msdata:IsDataSet=&quot;true&quot; msdata:EnforceConstraints=&quot;False&quot;> <xs:complexType>  <xs:element name=&quot;Customers&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;> <xs:complexType> <xs:sequence>  <xs:element name=&quot;workphone&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot; /> <xs:element name=&quot;homephone&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;cellphone&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> </xs:choice> <xs:element name=&quot;CustName&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;CustID&quot; type=&quot;xs:string&quot; /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> <xs:key name=&quot;RootKey1&quot;> <xs:selector xpath=&quot;.//mstns:Customers&quot; /> <xs:field xpath=&quot;mstns:CustID&quot; /> </xs:key> </xs:element> </xs:schema> </li> <li>On the File menu, click Save XMLSchema1 As.</li> <li>Name the file C:\MyTestConsoleApplication\customers.xsd, and then click Save.</li> <li>On the File menu, point to New, and then click File.</li> <li>Under Categories, click General, and then under Templates, click XML File. Click Open.</li> <li> Click the XML tab. Replace the existing code with the following XML code: <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <Root xmlns=&quot;http://tempuri.org/customer.xsd&quot;> <Customers> 9887858      4645645       8743857       2345678       <CustName>3saqib2</CustName> <CustID>32</CustID> </Customers> <Customers> 8763857      9876543       <CustName>Mian</CustName> <CustID>2</CustID> </Customers> </Root> </li> <li>On the File menu, click Save XMLFile1 As.</li> <li>Name the file C:\MyTestConsoleApplication\customers.xml, and then click Save.</li> <li>Start Visual Studio .NET Command Prompt.</li> <li> To generate a class file for the schema, type the following command at command prompt, and then press ENTER:

To Generate Visual C# .NET Class
xsd /c /l:cs /out:C:\MyTestConsoleApplication C:\MyTestConsoleApplication\customer.xsd

To Generate Visual Basic .NET Class
xsd /c /l:vb /out:C:\MyTestConsoleApplication C:\MyTestConsoleApplication\customer.xsd </li> <li>In Solution Explorer, click the Show All Files icon.</li> <li>Make sure that customer.cs, or customer.vb, is available in the list of files.</li> <li>Right-click customer.cs, or customer.vb, and then click Include In Project.</li> <li>On the Debug menu, click Start. You may notice multiple entries for cellphone and homephone in the Output window.</li> <li>Press ENTER to close the console.</li> <li> In XMLSchema1.xsd, locate the following choice element:  <xs:element name=&quot;workphone&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot; /> <xs:element name=&quot;homephone&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;cellphone&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> </xs:choice> Replace the choice element with the following choice elements:  <xs:element name=&quot;workphone&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot; /> </xs:choice>  <xs:element name=&quot;homephone&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;cellphone&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> </xs:choice> </li> <li>On the Debug menu, click Start.</li></ol>

<div class="references_section">