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 "More Information" section of this article:
<?xml version="1.0" ?> <xs:schema id="Root" targetNamespace="http://tempuri.org/customer.xsd" xmlns:mstns="http://tempuri.org/customer.xsd" xmlns="http://tempuri.org/customer.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified"> <xs:element name="Root" msdata:IsDataSet="true" msdata:EnforceConstraints="False"> <xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element name="Customers" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:choice minOccurs="0"> <xs:element name="workphone" type="xs:string" minOccurs="0" maxOccurs="unbounded" /> </xs:choice> <xs:choice minOccurs="0"> <xs:element name="homephone" type="xs:string" minOccurs="0" /> <xs:element name="cellphone" type="xs:string" minOccurs="0" /> </xs:choice> <xs:element name="CustName" type="xs:string" minOccurs="0" /> <xs:element name="CustID" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> <xs:key name="RootKey1"> <xs:selector xpath=".//mstns:Customers" /> <xs:field xpath="mstns:CustID" /> </xs:key> </xs:element> </xs:schema>
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 Behavior
- Start Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- Under Project Types, click Visual Basic Projects, or Visual C# Projects, and then under Templates, click Console Application.
- In the Name text box, type MyTestConsoleApplication, and in the Location text box, type C:\, and then click OK.
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(@"C:\MyTestConsoleApplication\customers.xml", 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("C:\MyTestConsoleApplication\customers.xml", 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
- On the File menu, point to New, and then click File.
- Under Categories, click General, and then under Templates, click XML Schema. Click Open.
Click the XML tab. Replace the existing code with the following XML Schema code:
<?xml version="1.0" ?> <xs:schema id="Root" targetNamespace="http://tempuri.org/customer.xsd" xmlns:mstns="http://tempuri.org/customer.xsd" xmlns="http://tempuri.org/customer.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified"> <xs:element name="Root" msdata:IsDataSet="true" msdata:EnforceConstraints="False"> <xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element name="Customers" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:choice minOccurs="0"> <xs:element name="workphone" type="xs:string" minOccurs="0" maxOccurs="unbounded" /> <xs:element name="homephone" type="xs:string" minOccurs="0" /> <xs:element name="cellphone" type="xs:string" minOccurs="0" /> </xs:choice> <xs:element name="CustName" type="xs:string" minOccurs="0" /> <xs:element name="CustID" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> <xs:key name="RootKey1"> <xs:selector xpath=".//mstns:Customers" /> <xs:field xpath="mstns:CustID" /> </xs:key> </xs:element> </xs:schema>
- On the File menu, click Save XMLSchema1 As.
- Name the file C:\MyTestConsoleApplication\customers.xsd, and then click Save.
- On the File menu, point to New, and then click File.
- Under Categories, click General, and then under Templates, click XML File. Click Open.
Click the XML tab. Replace the existing code with the following XML code:
<?xml version="1.0" encoding="utf-8" ?> <Root xmlns="http://tempuri.org/customer.xsd"> <Customers> <cellphone>9887858</cellphone> <cellphone>4645645</cellphone> <homephone>8743857</homephone> <homephone>2345678</homephone> <CustName>3saqib2</CustName> <CustID>32</CustID> </Customers> <Customers> <homephone>8763857</homephone> <cellphone>9876543</cellphone> <CustName>Mian</CustName> <CustID>2</CustID> </Customers> </Root>
- On the File menu, click Save XMLFile1 As.
- Name the file C:\MyTestConsoleApplication\customers.xml, and then click Save.
- Start Visual Studio .NET Command Prompt.
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
- In Solution Explorer, click the Show All Files icon.
- Make sure that customer.cs, or customer.vb, is available in the list of files.
- Right-click customer.cs, or customer.vb, and then click Include In Project.
- On the Debug menu, click Start. You may notice multiple entries for cellphone and homephone in the Output window.
- Press ENTER to close the console.
In XMLSchema1.xsd, locate the following choice element:
<xs:choice minOccurs="0"> <xs:element name="workphone" type="xs:string" minOccurs="0" maxOccurs="unbounded" /> <xs:element name="homephone" type="xs:string" minOccurs="0" /> <xs:element name="cellphone" type="xs:string" minOccurs="0" /> </xs:choice>
Replace the choice element with the following choice elements:
<xs:choice minOccurs="0"> <xs:element name="workphone" type="xs:string" minOccurs="0" maxOccurs="unbounded" /> </xs:choice> <xs:choice minOccurs="0"> <xs:element name="homephone" type="xs:string" minOccurs="0" /> <xs:element name="cellphone" type="xs:string" minOccurs="0" /> </xs:choice>
- On the Debug menu, click Start.
REFERENCES
For additional information about XML Schema in the .NET Framework, click the following article number to view the article in the Microsoft Knowledge Base:
313826 INFO: Roadmap for XML Schemas in the .NET Framework
Keywords: kbbug kbschema kbxml kbserial kbcodegen kbvs2002sp1sweep KB816226