Microsoft KB Archive/816222

From BetaArchive Wiki

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

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click File.
  3. Under Categories, click General, and then under Templates, click XML Schema. Click Open. By default, XMLSchema1 is created.
  4. 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>
  5. On the File menu, click Save FileName As, and then type C:\MyFirstXmlSchema.xsd in the File name text box. Click Save.
  6. On the File menu, point to New, and then click File.
  7. Under Categories, click General, and then under Templates, click XML Schema. Click Open.
  8. 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>
  9. On the File menu, click Save FileName As, type C:\MySecondXmlSchema.xsd in the File name text box, and then click Save.
  10. Start the Visual Studio .NET command prompt.
  11. 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
  12. 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;
    }
  13. 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
  14. 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