Microsoft KB Archive/814177

From BetaArchive Wiki

Article ID: 814177

Article Last Modified on 9/22/2003



APPLIES TO

  • Microsoft .NET Framework 1.0



SYMPTOMS

When you validate an empty XML instance document by using XmlValidatingReader, you may receive the following error message:

Element 'ElementName' has incomplete content. Expected 'Employee'. An error occurred at file :///FileName.



Where ElementName is the name of an empty element, and where FileName is the name of the XML instance document. You receive the error message even though in the schema file, the element has the nillable attribute set to true, and has a complex content. Additionally, the empty element in the instance document has the nill attribute set to true. The XmlValidatingReader ignores the nillable and nill attributes, and throws the error.


STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Because of this behavior, you may face other types of problems. For example, the schema validation of an empty DataTable may fail when validated with schema of the DataTable.

Steps to Reproduce the Behavior

  1. Copy the following XML code. Paste the code on a Notepad or any other text editor.

    <Employee xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
  2. Save the file as C:\EmpData.xml.
  3. Paste the following XML code in Notepad or any other text editor.

    <xs:schema id="Employee" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
      <xs:element name="Employee" nillable="true" >
        <xs:complexType>
          <xs:choice maxOccurs="unbounded">
            <xs:element name="PersonalDetails" >
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="emp_id" type="xs:string" minOccurs="0" />
                  <xs:element name="name" type="xs:string" minOccurs="0" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:schema>
  4. Save the file as C:\Employee.xsd.
  5. In Visual Studio .NET, create a new Visual Basic .NET Visual C# .NET Console Application project and name it MyXMLValidation.
  6. Replace the existing code in the code pane with the following code:

    Visual Basic .NET Code

    Imports System
    Imports System.Xml
    Imports System.Xml.Schema
    
    Module MyXMLValidation
    
        Sub Main()
            ' Create an XML reader to read XML file.
            Dim rd As XmlTextReader = New XmlTextReader("C:\EmpData.xml")
    
            ' Create a validating reader to validate XML file.
            Dim vr As XmlValidatingReader = New XmlValidatingReader(rd)
    
            ' Associate an event handler to trap Validation errors.
            AddHandler vr.ValidationEventHandler, AddressOf XmlValidationError
    
            ' Validate schema type.
            vr.ValidationType = ValidationType.Schema
    
            ' Load XML schema file.
            vr.Schemas.Add("", New XmlTextReader("C:\Employee.xsd"))
    
            ' Validate the XML file.
            While vr.Read()
                'Do Nothing.
            End While
            Console.WriteLine(vbCrLf & "Press Enter to close...")
            Console.Read()
        End Sub
    
        ' Handles XML Validation errors.
        Sub XmlValidationError(ByVal sender As Object, ByVal e As ValidationEventArgs)
            ' Display the error message on console.
            Console.WriteLine(e.Message)
        End Sub
    End Module


    Visual C# .NET Code

    using System;
    using System.Xml;
    using System.Xml.Schema;
            
    namespace MyXMLValidation
    {
        class Class1
        {
            static void Main()
            {
                // Create an XML reader to read XML file.
                XmlTextReader rd= new XmlTextReader("C:\\EmpData.xml");
            
                // Create a validating reader to validate XML file.
                XmlValidatingReader vr = new XmlValidatingReader(rd);
    
                // Associate an event handler to trap Validation errors.
                vr.ValidationEventHandler += new ValidationEventHandler(XmlValidationError);
    
                // Validate schema type.
                vr.ValidationType = ValidationType.Schema;
    
                // Load XML schema file.
                vr.Schemas.Add("", new XmlTextReader("C:\\Employee.xsd"));
            
                // Validate the XML file.
                while (vr.Read())
                {
                    // Do Nothing.
                }
                Console.WriteLine("\nPress Enter to close...");
                Console.Read();
            }
            
            // Handles XML Validation errors.
            static void XmlValidationError(object sender, ValidationEventArgs e)
            {
                // Display the error message on console.
                Console.WriteLine(e.Message);
            }
        }
    }   
  7. On the Debug menu, click Start to run the application.

In the console window, you receive the error message that is described in the "Symptoms" section of this article.

REFERENCES

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

308064 HOW TO: Persist an ADO.NET DataSet as XML by Using Visual Basic .NET


309183 HOW TO: Persist an ADO.NET DataSet as XML by Using Visual C# .NET


317595 HOW TO: Validate an XML Document by Using Multiple Schemas in Visual Basic .NET


318505 HOW TO: Validate an XML Document by Using Multiple Schemas in Visual C# .NET


The sample code works in .NET Framework version 1.1 without any change. Please add Microsoft .NET Framework Class Libraries 1.1 to the applies to section after .NET Framework 1.1 is released.

Use contents of Article: 326604 BUG: XSD validation fails on the DataSet with empty table for writing this article

Keywords: kberrmsg kbbug kbvalidation kbnamespace KB814177