ADO.NET limitation in validating XSD facets (811107)
The information in this article applies to:
- Microsoft ADO.NET (included with the .NET Framework)
- Microsoft ADO.Net 2.0
SYMPTOMS ADO.NET does not throw any exceptions when it validates
data that is newly inserted or modified in a DataRow. This is true if the
DataRow violates XSD facets that are defined in an underlying XSD schema of the
DataSet object. CAUSEADO.NET currently validates only the length and maxLength facets for the XSD string primitive data type. All other XSD
facets are ignored by ADO.NET when it validates data that is newly inserted or
modified in a DataRow.RESOLUTIONThis is currently a known limitation in ADO.NET.
You can work around this problem by using a System.Xml.XmlValidatingReader object to validate the XML representation of the data in a
DataSet against its underlying schema.STATUS
This behavior is by design.WORKAROUNDTo work around this problem, use a System.Xml.XmlValidatingReader object to validate the XML representation of the data in a
DataSet against its underlying schema.
- In Module1.vb, delete the existing code in Sub Main().
- In Module1.vb, paste the following code in Sub Main() . Study the in-line comments that explain the newly added code to
validate the XML representation of the data in the DataSet by using a System.Xml.XmlValidatingReader object:
Dim response As String
Try
Dim dsEmployees As System.Data.DataSet
dsEmployees = New DataSet()
Dim employeeTable As System.Data.DataTable
dsEmployees.ReadXmlSchema("c:\employee.xsd")
employeeTable = dsEmployees.Tables("Employee")
Dim employeeRow As System.Data.DataRow
employeeRow = employeeTable.NewRow
employeeRow("EmployeeID") = "E1"
employeeRow("FirstName") = "Jim"
employeeRow("MiddleInitial") = "A"
employeeRow("LastName") = "Stanton"
employeeRow("age") = 60
dsEmployees.Tables("Employee").Rows.Add(employeeRow)
'Write out the Dataset's schema to disk.
dsEmployees.WriteXmlSchema("c:\dsEmployeesSchema.xsd")
'Create an instance of an XmlValidatingReader to parse the XML representation of the Dataset's data
'(obtained by calling the GetXml method of the Dataset.
Dim rdr As New System.Xml.XmlValidatingReader(dsEmployees.GetXml, System.Xml.XmlNodeType.Element, Nothing)
'Add an event handler to trap validation errors.
AddHandler rdr.ValidationEventHandler, AddressOf ValidationEvent
'Create an instance of an XmlSchemaCollection object to cache the dataset's schema.
Dim sc As New System.Xml.Schema.XmlSchemaCollection()
rdr.ValidationType = Xml.ValidationType.Schema
'Add the XmlSchemaCollection to the XmlValidatingReaders schemas collection.
rdr.Schemas.Add(Nothing, "c:\dsEmployeesSchema.xsd")
'Parse the XML data.
'Any validation errors experienced are trapped and reported by the
'ValidationEvent custom sub procedure that implements the System.Xml.Schema.ValidationEventHandler delegate.
Do While rdr.Read
Loop
Catch exp As Exception
Console.WriteLine("Error occurred : " & exp.Message)
Finally
response = Console.ReadLine
End Try
- Add the following ValidationEvent custom Sub procedure to Module1.
This procedure is an
implementation of the System.Xml.Schema.ValidationEventHandler delegate. It is linked to the ValidationEventHandler event of the XmlValidatingReader object by the ADDHANDLER statement in Sub Main(). Any validation errors experienced by the XmlValidatingReader object as it parses the XML representation of the data in the
DataSet appears in the Console window by the code in this procedure: Public Sub ValidationEvent(ByVal sender As Object, ByVal args As System.Xml.Schema.ValidationEventArgs)
Console.WriteLine(args.Message)
End Sub
- Save, re-build, and then run the program. You receive the
following validation error message in the Console window to report the invalid
value assigned to the Age column. The error message displays the location of
the error as present in the source XML representation of the data in the
DataSet:
The 'age' element has an invalid value according to its
data type. An error occurred at (<line>,<col>).
The limitations of this workaround are that validation errors
related to constraints defined as XSD facets can be identified only after the
records are added to a DataTable; the error messages to report these errors are
XML based as opposed to being DataSet oriented. To locate the source
line in the XML that generated the error, write out and examine the DataSet's
XML (use the GetXml and WriteXml DataSet methods).You also have to use XmlValidatingReader to validate the data. This is the only known workaround to
address this bug, and it can be used effectively to validate the data before
updating a target data source with data that is not valid.
Modification Type: | Minor | Last Reviewed: | 3/9/2006 |
---|
Keywords: | kbbug KB811107 kbAudDeveloper |
---|
|