Microsoft KB Archive/816222: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - ">" to ">")
 
(One intermediate revision by the same user not shown)
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> element in the including schema is set to '''qualified''', even though the included schema sets the '''elementFormDefault''' attribute to '''unqualified'''.
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 &lt;xs:schema&gt; 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 &lt;xs:schema&gt; element of the schema is set to '''qualified''', '''XmlElementAttribute''' is not generated for the complex type member elements of the included 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">&lt;System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)&gt; _</pre>
<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">&lt;xs:schema attributeFormDefault=&quot;qualified&quot; elementFormDefault=&quot;unqualified&quot; targetNamespace=&quot;mainns&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
<pre class="codesample"><xs:schema attributeFormDefault=&quot;qualified&quot; elementFormDefault=&quot;unqualified&quot; targetNamespace=&quot;mainns&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;>
   &lt;xs:complexType name=&quot;Employee&quot; mixed=&quot;true&quot;&gt;
   <xs:complexType name=&quot;Employee&quot; mixed=&quot;true&quot;>
       &lt;xs:sequence&gt;
       <xs:sequence>
         &lt;xs:element name=&quot;IntegerElement&quot; type=&quot;xs:int&quot; /&gt;
         <xs:element name=&quot;IntegerElement&quot; type=&quot;xs:int&quot; />
         &lt;xs:any minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot; /&gt;
         <xs:any minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot; />
       &lt;/xs:sequence&gt;
       </xs:sequence>
       &lt;xs:attribute name=&quot;TestAttribute&quot; type=&quot;xs:int&quot; /&gt;
       <xs:attribute name=&quot;TestAttribute&quot; type=&quot;xs:int&quot; />
   &lt;/xs:complexType&gt;
   </xs:complexType>
   &lt;xs:complexType name=&quot;EmpDataType&quot; mixed=&quot;false&quot;&gt;
   <xs:complexType name=&quot;EmpDataType&quot; mixed=&quot;false&quot;>
       &lt;xs:sequence&gt;
       <xs:sequence>
         &lt;xs:element name=&quot;EmpId&quot; type=&quot;xs:int&quot; /&gt;
         <xs:element name=&quot;EmpId&quot; type=&quot;xs:int&quot; />
         &lt;xs:element name=&quot;Salary&quot; type=&quot;xs:float&quot; /&gt;
         <xs:element name=&quot;Salary&quot; type=&quot;xs:float&quot; />
         &lt;xs:element name=&quot;EmployeeName&quot; type=&quot;xs:string&quot; /&gt;
         <xs:element name=&quot;EmployeeName&quot; type=&quot;xs:string&quot; />
       &lt;/xs:sequence&gt;
       </xs:sequence>
   &lt;/xs:complexType&gt;
   </xs:complexType>
   &lt;xs:element name=&quot;MyComplexElement&quot; type=&quot;main:EmpDataType&quot; xmlns:main=&quot;mainns&quot; /&gt;
   <xs:element name=&quot;MyComplexElement&quot; type=&quot;main:EmpDataType&quot; xmlns:main=&quot;mainns&quot; />
&lt;/xs:schema&gt;</pre></li>
</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">&lt;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;&gt;
<pre class="codesample"><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;>
   &lt;xs:include schemaLocation=&quot;C:\MyFirstXMLSchema.xsd&quot; /&gt;
   <xs:include schemaLocation=&quot;C:\MyFirstXMLSchema.xsd&quot; />
   &lt;xs:element name=&quot;MyMainElement&quot; type=&quot;mstns:EmpDataType&quot; /&gt;
   <xs:element name=&quot;MyMainElement&quot; type=&quot;mstns:EmpDataType&quot; />
&lt;/xs:schema&gt;</pre></li>
</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:
'
'


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




/// &lt;remarks/&gt;
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespace=&quot;mainns&quot;)]
[System.Xml.Serialization.XmlTypeAttribute(Namespace=&quot;mainns&quot;)]
[System.Xml.Serialization.XmlRootAttribute(&quot;MyComplexElement&quot;, Namespace=&quot;mainns&quot;, IsNullable=false)]
[System.Xml.Serialization.XmlRootAttribute(&quot;MyComplexElement&quot;, Namespace=&quot;mainns&quot;, IsNullable=false)]
public class EmpDataType {
public class EmpDataType {
      
      
     /// &lt;remarks/&gt;
     /// <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;
      
      
     /// &lt;remarks/&gt;
     /// <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;
      
      
     /// &lt;remarks/&gt;
     /// <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:
'
'


'&lt;remarks/&gt;
'<remarks/>
&lt;System.Xml.Serialization.XmlTypeAttribute([Namespace]:=&quot;mainns&quot;),  _
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:=&quot;mainns&quot;),  _
  System.Xml.Serialization.XmlRootAttribute(&quot;MyMainElement&quot;, [Namespace]:=&quot;mainns&quot;, IsNullable:=false)&gt; _
  System.Xml.Serialization.XmlRootAttribute(&quot;MyMainElement&quot;, [Namespace]:=&quot;mainns&quot;, IsNullable:=false)> _
Public Class EmpDataType
Public Class EmpDataType
      
      
     '&lt;remarks/&gt;
     '<remarks/>
     Public EmpId As Integer
     Public EmpId As Integer
      
      
     '&lt;remarks/&gt;
     '<remarks/>
     Public Salary As Single
     Public Salary As Single
      
      
     '&lt;remarks/&gt;
     '<remarks/>
     Public EmployeeName As String
     Public EmployeeName As String
End Class</pre>
End Class</pre>
Line 210: Line 210:




/// &lt;remarks/&gt;
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespace=&quot;mainns&quot;)]
[System.Xml.Serialization.XmlTypeAttribute(Namespace=&quot;mainns&quot;)]
[System.Xml.Serialization.XmlRootAttribute(&quot;MyMainElement&quot;, Namespace=&quot;mainns&quot;, IsNullable=false)]
[System.Xml.Serialization.XmlRootAttribute(&quot;MyMainElement&quot;, Namespace=&quot;mainns&quot;, IsNullable=false)]
public class EmpDataType {
public class EmpDataType {
      
      
     /// &lt;remarks/&gt;
     /// <remarks/>
     public int EmpId;
     public int EmpId;
      
      
     /// &lt;remarks/&gt;
     /// <remarks/>
     public System.Single Salary;
     public System.Single Salary;
      
      
     /// &lt;remarks/&gt;
     /// <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

  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