Microsoft KB Archive/814177

= Bug: XmlValidatingReader Ignores the Schema Nillable and the XML Nill Attributes =

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 ' ' has incomplete content. Expected 'Employee'. An error occurred at file :///FileName.

Where  is the name of an empty element, and where   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
  Copy the following XML code. Paste the code on a Notepad or any other text editor.   Save the file as C:\EmpData.xml .  Paste the following XML code in Notepad or any other text editor.       <xs:sequence> <xs:element name=&quot;emp_id&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> <xs:element name=&quot;name&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> </li> Save the file as C:\Employee.xsd .</li> In Visual Studio .NET, create a new Visual Basic .NET Visual C# .NET Console Application project and name it MyXMLValidation .</li>  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(&quot;C:\EmpData.xml&quot;)

' 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(&quot;&quot;, New XmlTextReader(&quot;C:\Employee.xsd&quot;))

' Validate the XML file. While vr.Read 'Do Nothing. End While Console.WriteLine(vbCrLf & &quot;Press Enter to close...&quot;) 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(&quot;C:\\EmpData.xml&quot;); // 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(&quot;&quot;, new XmlTextReader(&quot;C:\\Employee.xsd&quot;)); // Validate the XML file. while (vr.Read) {               // Do Nothing. }           Console.WriteLine(&quot;\nPress Enter to close...&quot;); Console.Read; }       // Handles XML Validation errors. static void XmlValidationError(object sender, ValidationEventArgs e)       { // Display the error message on console. Console.WriteLine(e.Message); }   } }   </li> On the Debug menu, click Start to run the application.</li></ol>

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

<div class="references_section">

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. <pre class="fixed_text">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

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.