Microsoft KB Archive/816222

= PRB: XmlElementAttribute Is Missing for the Generated Class Members While Using Xsd.exe =

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  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  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  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
 _

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=&quot;qualified&quot; elementFormDefault=&quot;unqualified&quot; targetNamespace=&quot;mainns&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xs:complexType name=&quot;Employee&quot; mixed=&quot;true&quot;> <xs:sequence> <xs:element name=&quot;IntegerElement&quot; type=&quot;xs:int&quot; /> <xs:any minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot; /> </xs:sequence> <xs:attribute name=&quot;TestAttribute&quot; type=&quot;xs:int&quot; /> </xs:complexType> <xs:complexType name=&quot;EmpDataType&quot; mixed=&quot;false&quot;> <xs:sequence> <xs:element name=&quot;EmpId&quot; type=&quot;xs:int&quot; /> <xs:element name=&quot;Salary&quot; type=&quot;xs:float&quot; /> <xs:element name=&quot;EmployeeName&quot; type=&quot;xs:string&quot; /> </xs:sequence> </xs:complexType> <xs:element name=&quot;MyComplexElement&quot; type=&quot;main:EmpDataType&quot; xmlns:main=&quot;mainns&quot; /> </xs:schema> </li> On the File menu, click Save FileName As, and then type C:\MyFirstXmlSchema.xsd in the File name text box. Click Save.</li> On the File menu, point to New, and then click File.</li> Under Categories, click General, and then under Templates, click XML Schema. Click Open.</li>  On the XML tab, replace the existing code with the following code: <xs:schema attributeFormDefault=&quot;qualified&quot; elementFormDefault=&quot;qualified&quot; targetNamespace=&quot;mainns&quot; xmlns=&quot;mainns&quot; xmlns:mstns=&quot;mainns&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xs:include schemaLocation=&quot;C:\MyFirstXMLSchema.xsd&quot; /> <xs:element name=&quot;MyMainElement&quot; type=&quot;mstns:EmpDataType&quot; /> </xs:schema> </li> On the File menu, click Save FileName As, type C:\MySecondXmlSchema.xsd in the File name text box, and then click Save.</li> Start the Visual Studio .NET command prompt.</li>  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 .NET xsd /c /l:vb /o:C:\ C:\MyFirstXmlSchema.xsd To generate classes in Visual C# .NET xsd /c /l:cs /o:C:\ C:\MyFirstXmlSchema.xsd </li>  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. '

' <System.Xml.Serialization.XmlTypeAttribute([Namespace]:=&quot;mainns&quot;), _ System.Xml.Serialization.XmlRootAttribute(&quot;MyComplexElement&quot;, [Namespace]:=&quot;mainns&quot;, IsNullable:=false)> _ Public Class EmpDataType '    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _ Public EmpId As Integer '    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _ Public Salary As Single '    <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;

/// [System.Xml.Serialization.XmlTypeAttribute(Namespace=&quot;mainns&quot;)] [System.Xml.Serialization.XmlRootAttribute(&quot;MyComplexElement&quot;, Namespace=&quot;mainns&quot;, IsNullable=false)] public class EmpDataType { ///    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public int EmpId; ///    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public System.Single Salary; ///    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string EmployeeName; } </li>  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 .NET xsd /c /l:vb /o:C:\ C:\MySecondXmlSchema.xsd To generate classes in Visual C# .NET xsd /c /l:cs /o:C:\ C:\MySecondXmlSchema.xsd </li>  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. '

' <System.Xml.Serialization.XmlTypeAttribute([Namespace]:=&quot;mainns&quot;), _ System.Xml.Serialization.XmlRootAttribute(&quot;MyMainElement&quot;, [Namespace]:=&quot;mainns&quot;, IsNullable:=false)> _ Public Class EmpDataType '    Public EmpId As Integer '    Public Salary As Single '    Public EmployeeName As String End Class Visual C# .NET (C:\MySecondXmlSchema.xsd) using System.Xml.Serialization;

/// [System.Xml.Serialization.XmlTypeAttribute(Namespace=&quot;mainns&quot;)] [System.Xml.Serialization.XmlRootAttribute(&quot;MyMainElement&quot;, Namespace=&quot;mainns&quot;, IsNullable=false)] public class EmpDataType { ///    public int EmpId; ///    public System.Single Salary; ///    public string EmployeeName; } </li></ol>

Notice the XmlElementAttribute is missing in the class that is generated in step 14.

<div class="references_section">