Microsoft KB Archive/816222: Difference between revisions
m (Text replacement - "<" to "<") |
m (Text replacement - ">" to ">") |
||
Line 44: | Line 44: | ||
== SYMPTOMS == | == SYMPTOMS == | ||
When you use the XML Schema Definition Tool (Xsd.exe) to generate managed classes from an XML schema, the '''XmlElementAttribute''' class is not generated for complex type member elements of the top-level elements of an included schema. You see this behavior when the value of the '''elementFormDefault''' attribute of the <xs:schema | When you use the XML Schema Definition Tool (Xsd.exe) to generate managed classes from an XML schema, the '''XmlElementAttribute''' class is not generated for complex type member elements of the top-level elements of an included schema. You see this behavior when the value of the '''elementFormDefault''' attribute of the <xs:schema> element in the including schema is set to '''qualified''', even though the included schema sets the '''elementFormDefault''' attribute to '''unqualified'''. | ||
</div> | </div> | ||
Line 51: | Line 51: | ||
== CAUSE == | == CAUSE == | ||
This behavior occurs when the '''elementFormDefault''' attribute of the element of the including schema <xs:schema | This behavior occurs when the '''elementFormDefault''' attribute of the element of the including schema <xs:schema> is set to '''qualified'''. While processing the XML schema includes, the schema compiler combines the schemas in the include hierarchy to generate a Single Compiled Schema (SOM). The '''elementFormDefault''' and '''attributeFormDefault''' schema settings for compiled schema reflect the settings that are specified for the top-most schema in the include hierarchy. Therefore, when the '''elementFormDefault''' attribute of the top-most <xs:schema> element of the schema is set to '''qualified''', '''XmlElementAttribute''' is not generated for the complex type member elements of the included schema. | ||
</div> | </div> | ||
Line 62: | Line 62: | ||
==== Visual Basic .NET Code ==== | ==== Visual Basic .NET Code ==== | ||
<pre class="codesample"><System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified) | <pre class="codesample"><System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _</pre> | ||
==== Visual C# .NET Code ==== | ==== Visual C# .NET Code ==== | ||
Line 91: | Line 91: | ||
<li>Under '''Categories''', click '''General''', and then under '''Templates''', click '''XML Schema'''. Click '''Open'''. By default, XMLSchema1 is created.</li> | <li>Under '''Categories''', click '''General''', and then under '''Templates''', click '''XML Schema'''. Click '''Open'''. By default, XMLSchema1 is created.</li> | ||
<li><p>On the '''XML''' tab, replace the existing code with the following code:</p> | <li><p>On the '''XML''' tab, replace the existing code with the following code:</p> | ||
<pre class="codesample"><xs:schema attributeFormDefault="qualified" elementFormDefault="unqualified" targetNamespace="mainns" xmlns:xs="http://www.w3.org/2001/XMLSchema" | <pre class="codesample"><xs:schema attributeFormDefault="qualified" elementFormDefault="unqualified" targetNamespace="mainns" xmlns:xs="http://www.w3.org/2001/XMLSchema"> | ||
<xs:complexType name="Employee" mixed="true" | <xs:complexType name="Employee" mixed="true"> | ||
<xs:sequence | <xs:sequence> | ||
<xs:element name="IntegerElement" type="xs:int" / | <xs:element name="IntegerElement" type="xs:int" /> | ||
<xs:any minOccurs="0" maxOccurs="unbounded" / | <xs:any minOccurs="0" maxOccurs="unbounded" /> | ||
</xs:sequence | </xs:sequence> | ||
<xs:attribute name="TestAttribute" type="xs:int" / | <xs:attribute name="TestAttribute" type="xs:int" /> | ||
</xs:complexType | </xs:complexType> | ||
<xs:complexType name="EmpDataType" mixed="false" | <xs:complexType name="EmpDataType" mixed="false"> | ||
<xs:sequence | <xs:sequence> | ||
<xs:element name="EmpId" type="xs:int" / | <xs:element name="EmpId" type="xs:int" /> | ||
<xs:element name="Salary" type="xs:float" / | <xs:element name="Salary" type="xs:float" /> | ||
<xs:element name="EmployeeName" type="xs:string" / | <xs:element name="EmployeeName" type="xs:string" /> | ||
</xs:sequence | </xs:sequence> | ||
</xs:complexType | </xs:complexType> | ||
<xs:element name="MyComplexElement" type="main:EmpDataType" xmlns:main="mainns" / | <xs:element name="MyComplexElement" type="main:EmpDataType" xmlns:main="mainns" /> | ||
</xs:schema | </xs:schema></pre></li> | ||
<li>On the '''File''' menu, click '''Save FileName As''', and then type <span class="kbd userinput"> C:\MyFirstXmlSchema.xsd</span> in the '''File name''' text box. Click '''Save'''.</li> | <li>On the '''File''' menu, click '''Save FileName As''', and then type <span class="kbd userinput"> C:\MyFirstXmlSchema.xsd</span> in the '''File name''' text box. Click '''Save'''.</li> | ||
<li>On the '''File''' menu, point to '''New''', and then click '''File'''.</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>Under '''Categories''', click '''General''', and then under '''Templates''', click '''XML Schema'''. Click '''Open'''.</li> | ||
<li><p>On the '''XML''' tab, replace the existing code with the following code:</p> | <li><p>On the '''XML''' tab, replace the existing code with the following code:</p> | ||
<pre class="codesample"><xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="mainns" xmlns="mainns" xmlns:mstns="mainns" xmlns:xs="http://www.w3.org/2001/XMLSchema" | <pre class="codesample"><xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="mainns" xmlns="mainns" xmlns:mstns="mainns" xmlns:xs="http://www.w3.org/2001/XMLSchema"> | ||
<xs:include schemaLocation="C:\MyFirstXMLSchema.xsd" / | <xs:include schemaLocation="C:\MyFirstXMLSchema.xsd" /> | ||
<xs:element name="MyMainElement" type="mstns:EmpDataType" / | <xs:element name="MyMainElement" type="mstns:EmpDataType" /> | ||
</xs:schema | </xs:schema></pre></li> | ||
<li>On the '''File''' menu, click '''Save FileName As''', type <span class="kbd userinput"> C:\MySecondXmlSchema.xsd</span> in the '''File name''' text box, and then click '''Save'''.</li> | <li>On the '''File''' menu, click '''Save FileName As''', type <span class="kbd userinput"> C:\MySecondXmlSchema.xsd</span> in the '''File name''' text box, and then click '''Save'''.</li> | ||
<li>Start the Visual Studio .NET command prompt.</li> | <li>Start the Visual Studio .NET command prompt.</li> | ||
Line 136: | Line 136: | ||
' | ' | ||
'<remarks/ | '<remarks/> | ||
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="mainns"), _ | <System.Xml.Serialization.XmlTypeAttribute([Namespace]:="mainns"), _ | ||
System.Xml.Serialization.XmlRootAttribute("MyComplexElement", [Namespace]:="mainns", IsNullable:=false) | System.Xml.Serialization.XmlRootAttribute("MyComplexElement", [Namespace]:="mainns", IsNullable:=false)> _ | ||
Public Class EmpDataType | Public Class EmpDataType | ||
'<remarks/ | '<remarks/> | ||
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified) | <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _ | ||
Public EmpId As Integer | Public EmpId As Integer | ||
'<remarks/ | '<remarks/> | ||
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified) | <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _ | ||
Public Salary As Single | Public Salary As Single | ||
'<remarks/ | '<remarks/> | ||
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified) | <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _ | ||
Public EmployeeName As String | Public EmployeeName As String | ||
End Class</pre> | End Class</pre> | ||
Line 157: | Line 157: | ||
/// <remarks/ | /// <remarks/> | ||
[System.Xml.Serialization.XmlTypeAttribute(Namespace="mainns")] | [System.Xml.Serialization.XmlTypeAttribute(Namespace="mainns")] | ||
[System.Xml.Serialization.XmlRootAttribute("MyComplexElement", Namespace="mainns", IsNullable=false)] | [System.Xml.Serialization.XmlRootAttribute("MyComplexElement", Namespace="mainns", IsNullable=false)] | ||
public class EmpDataType { | public class EmpDataType { | ||
/// <remarks/ | /// <remarks/> | ||
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] | [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] | ||
public int EmpId; | public int EmpId; | ||
/// <remarks/ | /// <remarks/> | ||
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] | [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] | ||
public System.Single Salary; | public System.Single Salary; | ||
/// <remarks/ | /// <remarks/> | ||
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] | [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] | ||
public string EmployeeName; | public string EmployeeName; | ||
Line 192: | Line 192: | ||
' | ' | ||
'<remarks/ | '<remarks/> | ||
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="mainns"), _ | <System.Xml.Serialization.XmlTypeAttribute([Namespace]:="mainns"), _ | ||
System.Xml.Serialization.XmlRootAttribute("MyMainElement", [Namespace]:="mainns", IsNullable:=false) | System.Xml.Serialization.XmlRootAttribute("MyMainElement", [Namespace]:="mainns", IsNullable:=false)> _ | ||
Public Class EmpDataType | Public Class EmpDataType | ||
'<remarks/ | '<remarks/> | ||
Public EmpId As Integer | Public EmpId As Integer | ||
'<remarks/ | '<remarks/> | ||
Public Salary As Single | Public Salary As Single | ||
'<remarks/ | '<remarks/> | ||
Public EmployeeName As String | Public EmployeeName As String | ||
End Class</pre> | End Class</pre> | ||
Line 210: | Line 210: | ||
/// <remarks/ | /// <remarks/> | ||
[System.Xml.Serialization.XmlTypeAttribute(Namespace="mainns")] | [System.Xml.Serialization.XmlTypeAttribute(Namespace="mainns")] | ||
[System.Xml.Serialization.XmlRootAttribute("MyMainElement", Namespace="mainns", IsNullable=false)] | [System.Xml.Serialization.XmlRootAttribute("MyMainElement", Namespace="mainns", IsNullable=false)] | ||
public class EmpDataType { | public class EmpDataType { | ||
/// <remarks/ | /// <remarks/> | ||
public int EmpId; | public int EmpId; | ||
/// <remarks/ | /// <remarks/> | ||
public System.Single Salary; | public System.Single Salary; | ||
/// <remarks/ | /// <remarks/> | ||
public string EmployeeName; | public string EmployeeName; | ||
}</pre></li></ol> | }</pre></li></ol> |
Latest revision as of 10:21, 21 July 2020
Article ID: 816222
Article Last Modified on 5/14/2003
APPLIES TO
- Microsoft .NET Framework 1.0
- Microsoft .NET Framework 1.1
- Microsoft Visual C# .NET 2002 Standard Edition
- Microsoft Visual C# .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
- Microsoft Visual Basic .NET 2003 Standard Edition
SYMPTOMS
When you use the XML Schema Definition Tool (Xsd.exe) to generate managed classes from an XML schema, the XmlElementAttribute class is not generated for complex type member elements of the top-level elements of an included schema. You see this behavior when the value of the elementFormDefault attribute of the <xs:schema> element in the including schema is set to qualified, even though the included schema sets the elementFormDefault attribute to unqualified.
CAUSE
This behavior occurs when the elementFormDefault attribute of the element of the including schema <xs:schema> is set to qualified. While processing the XML schema includes, the schema compiler combines the schemas in the include hierarchy to generate a Single Compiled Schema (SOM). The elementFormDefault and attributeFormDefault schema settings for compiled schema reflect the settings that are specified for the top-most schema in the include hierarchy. Therefore, when the elementFormDefault attribute of the top-most <xs:schema> element of the schema is set to qualified, XmlElementAttribute is not generated for the complex type member elements of the included schema.
WORKAROUND
To work around this problem, manually add the XmlElementAttribute class to the complex type element members of base schema. Add XmlElementAttribute only when you must serialize the members as unqualified elements when an instance of the type is serialized using the XmlSerializer. To do so, add the following statement before each member of the generated class:
Visual Basic .NET Code
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Visual C# .NET Code
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
Note No changes are required in the generated class when you are not concerned about the serialization of the members as qualified elements.
STATUS
This behavior is by design.
MORE INFORMATION
When the value of the elementFormDefault attribute for a schema is set to qualified, the XmlElementAttribute is missing from the generated class. This behavior occurs because of the following:
- By default, public properties and fields of a class are serialized as XML elements when you use XmlSerializer to serialize an instance of the class.
- The default value of the Form property of the XmlElementAttribute is qualified.
Steps to Reproduce the Behavior
- Start Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click File.
- Under Categories, click General, and then under Templates, click XML Schema. Click Open. By default, XMLSchema1 is created.
On the XML tab, replace the existing code with the following code:
<xs:schema attributeFormDefault="qualified" elementFormDefault="unqualified" targetNamespace="mainns" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="Employee" mixed="true"> <xs:sequence> <xs:element name="IntegerElement" type="xs:int" /> <xs:any minOccurs="0" maxOccurs="unbounded" /> </xs:sequence> <xs:attribute name="TestAttribute" type="xs:int" /> </xs:complexType> <xs:complexType name="EmpDataType" mixed="false"> <xs:sequence> <xs:element name="EmpId" type="xs:int" /> <xs:element name="Salary" type="xs:float" /> <xs:element name="EmployeeName" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:element name="MyComplexElement" type="main:EmpDataType" xmlns:main="mainns" /> </xs:schema>
- On the File menu, click Save FileName As, and then type C:\MyFirstXmlSchema.xsd in the File name text box. Click Save.
- On the File menu, point to New, and then click File.
- Under Categories, click General, and then under Templates, click XML Schema. Click Open.
On the XML tab, replace the existing code with the following code:
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="mainns" xmlns="mainns" xmlns:mstns="mainns" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:include schemaLocation="C:\MyFirstXMLSchema.xsd" /> <xs:element name="MyMainElement" type="mstns:EmpDataType" /> </xs:schema>
- On the File menu, click Save FileName As, type C:\MySecondXmlSchema.xsd in the File name text box, and then click Save.
- Start the Visual Studio .NET command prompt.
To generate the classes from the base schema, type the following command at the command prompt, and then press ENTER:
To generate classes in Visual Basic .NETxsd /c /l:vb /o:C:\ C:\MyFirstXmlSchema.xsd
To generate classes in Visual C# .NET
xsd /c /l:cs /o:C:\ C:\MyFirstXmlSchema.xsd
The class is generated as follows:
Visual Basic .NET (C:\MyFirstXmlSchema.xsd)Option Strict Off Option Explicit On Imports System.Xml.Serialization ' 'This source code was auto-generated by xsd, Version=1.0.3705.0. ' '<remarks/> <System.Xml.Serialization.XmlTypeAttribute([Namespace]:="mainns"), _ System.Xml.Serialization.XmlRootAttribute("MyComplexElement", [Namespace]:="mainns", IsNullable:=false)> _ Public Class EmpDataType '<remarks/> <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _ Public EmpId As Integer '<remarks/> <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _ Public Salary As Single '<remarks/> <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _ Public EmployeeName As String End Class
Visual C# .NET (C:\MyFirstXmlSchema.xsd)
using System.Xml.Serialization; /// <remarks/> [System.Xml.Serialization.XmlTypeAttribute(Namespace="mainns")] [System.Xml.Serialization.XmlRootAttribute("MyComplexElement", Namespace="mainns", IsNullable=false)] public class EmpDataType { /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public int EmpId; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public System.Single Salary; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string EmployeeName; }
To generate the classes from the including schema, type the following command at command prompt, and then press ENTER:
To generate classes in Visual Basic .NETxsd /c /l:vb /o:C:\ C:\MySecondXmlSchema.xsd
To generate classes in Visual C# .NET
xsd /c /l:cs /o:C:\ C:\MySecondXmlSchema.xsd
The class is generated as follows:
Visual Basic .NET (C:\MySecondXmlSchema.xsd)Option Strict Off Option Explicit On Imports System.Xml.Serialization ' 'This source code was auto-generated by xsd, Version=1.0.3705.0. ' '<remarks/> <System.Xml.Serialization.XmlTypeAttribute([Namespace]:="mainns"), _ System.Xml.Serialization.XmlRootAttribute("MyMainElement", [Namespace]:="mainns", IsNullable:=false)> _ Public Class EmpDataType '<remarks/> Public EmpId As Integer '<remarks/> Public Salary As Single '<remarks/> Public EmployeeName As String End Class
Visual C# .NET (C:\MySecondXmlSchema.xsd)
using System.Xml.Serialization; /// <remarks/> [System.Xml.Serialization.XmlTypeAttribute(Namespace="mainns")] [System.Xml.Serialization.XmlRootAttribute("MyMainElement", Namespace="mainns", IsNullable=false)] public class EmpDataType { /// <remarks/> public int EmpId; /// <remarks/> public System.Single Salary; /// <remarks/> public string EmployeeName; }
Notice the XmlElementAttribute is missing in the class that is generated in step 14.
REFERENCES
For additional information about XML Schema, click the following article number to view the article in the Microsoft Knowledge Base:
313826 INFO: Roadmap for XML Schemas in the .NET
Keywords: kbschema kbxml kbprb kbcodegen KB816222