PRB: "Schema Validation Warning" When You Generate a Managed Class for XML Schema with an any Element Declaration and a Local Element Declaration (815108)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

SYMPTOMS

When you create an XML Schema that contains an any element without a namespace declaration, or if the value for the namespace is set to any value other than ##other and ##local along with a local element declaration, you may receive the following warnings when you generate managed classes for the schema by using the XML Schema Definition Tool (Xsd.exe):
Schema validation warning: The content model must be deterministic. Wildcard dec laration along with a local element declaration causes content model to become a mbiguous. An error occurred at file:Filename.xsd(Line, Column).
Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.

CAUSE

The any element permits any element from the specified namespace to appear in the containing sequence or the choice element. Xsd.exe does not support the namespace for the any element and the warning messages display when you generate managed classes.

WORKAROUND

To work around this problem, do not declare local elements when you declare an any element. When you set the value for the namespace for the any element as "##other" or "##local", you may not get warnings when you generate managed classes. However, when you build a library for the generated managed class and then generate schema from the library by using XSD.exe, you may not get the namespace declaration in the generated schema.

For more information about XML Schema design patterns, visit the following MSDN Web site:

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click File.
  3. Select General under Categories, select XML Schema under Templates, and then click Open. By default, XMLSchema1 is created.
  4. On the XML tab, replace the existing code with the following code:
    <?xml version="1.0" encoding="utf-8" ?>
    <xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" 
    xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd"  
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
       <xs:element name="Root">
          <xs:complexType mixed="true">
             <xs:sequence>
                <xs:any minOccurs="0" />
                <xs:element name="MyElement" type="xs:string" />
             </xs:sequence>
             <xs:anyAttribute />
          </xs:complexType>
       </xs:element>
    </xs:schema>
  5. On the File menu, click Save FileName As.
  6. Type C:\MyXmlSchema.xsd as the File name, and then click Save.
  7. Start Visual Studio .NET Command Prompt.
  8. To generate the classes, type the following command at the command prompt and then press ENTER key:

    In Microsoft Visual Basic .NET
    xsd /c /l:vb /o:C:\ C:\MyXmlSchema.xsd
    In Microsoft Visual C# .NET
    xsd /c /l:cs /o:C:\ C:\MyXmlSchema.xsd
  9. You may notice the warnings that are described in the "Symptoms" section of this article.

    The following managed class is generated:

    Visual Basic .NET
    '------------------------------------------------------------------------------
    ' <autogenerated>
    '     This code was generated by a tool.
    '     Runtime Version: 1.0.3705.0
    '
    '     Changes to this file may cause incorrect behavior and will be lost if 
    '     the code is regenerated.
    ' </autogenerated>
    '------------------------------------------------------------------------------
    
    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]:="http://tempuri.org/XMLSchema.xsd"),  _
     System.Xml.Serialization.XmlRootAttribute([Namespace]:="http://tempuri.org/XMLSchema.xsd", IsNullable:=false)>  _
    Public Class Root
        
        '<remarks/>
        <System.Xml.Serialization.XmlTextAttribute(),  _
         System.Xml.Serialization.XmlAnyElementAttribute()>  _
        Public Any() As System.Xml.XmlNode
        
        '<remarks/>
        Public MyElement As String
        
        '<remarks/>
        <System.Xml.Serialization.XmlAnyAttributeAttribute()>  _
        Public AnyAttr() As System.Xml.XmlAttribute
    End Class
    
    Visual C# .NET
    //------------------------------------------------------------------------------
    // <autogenerated>
    //     This code was generated by a tool.
    //     Runtime Version: 1.0.3705.0
    //
    //     Changes to this file may cause incorrect behavior and will be lost if 
    //     the code is regenerated.
    // </autogenerated>
    //------------------------------------------------------------------------------
    
    // 
    // This source code was auto-generated by xsd, Version=1.0.3705.0.
    // 
    using System.Xml.Serialization;
    
    
    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/XMLSchema.xsd")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/XMLSchema.xsd", IsNullable=false)]
    public class Root {
        
        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        [System.Xml.Serialization.XmlAnyElementAttribute()]
        public System.Xml.XmlNode[] Any;
        
        /// <remarks/>
        public string MyElement;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlAnyAttributeAttribute()]
        public System.Xml.XmlAttribute[] AnyAttr;
    }
    

REFERENCES

For additional information about XML Schemas in the Microsoft .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


Modification Type:MajorLast Reviewed:9/19/2003
Keywords:kbCodeGen kbXML kbSchema kbprb KB815108 kbAudDeveloper