Sucessfull validation (using XmlReader) of a crazy file?
The current recommended way to validate an XML file is by using schemas stored in theXmlSchemaSet. "The namespace in the XML file,urn:bookstore-schema, identifies which schema in theXmlSchemaSet to use for validation." Butif you have to deal with a XML file without a namespace you have to add it's correspondent schema to the schemaSet using null/nothing. Like this:
sc.Add(nothing , "books.xsd")
And it works. But if, after that, you try to validate a crazy file with a different namespace fie and a completely different format, you still get sucess! (Although you had set validationType to schema).
So, at the moment, XmlReader it's not trustable when we are not sure of what is the namespace in the XML. We have to check it out, manually, after the first read (looking atreader.NamespaceURI, for instance).
I hardly believe I'm right. Can anyone tell me why I'm wrong?
Well, I'm trying to stay on version 2. Microsoft states: "The
XmlValidatingReader class is obsolete in.NET Framework version 2.0. You can create a validating
XmlReader instance using the
XmlReaderSettings class and the
Create method. For more information, see
Validating XML Data with XmlReader."
Thanks any way. I'll keep looking for a solution within version 2
When validating an xml file whose namespace is not present in the XmlSchemaSet at all, validating this file against the schema set should create warnings of the form "Could not find schema information for the element xxx".
Warnings are generated instead of errors since we start with lax validation at the root element of the XML until we find schema information.
When creating an XmlReader using XmlReaderSettings (by setting ValidationType = ValidationType.Schema), there are some settings on the ValidationFlags property that control whether warnings are generated or not. By default warnings are turned off and that is the reason you are not seeing the warnings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null,"books.xsd");
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
XmlReader validatingReader = XmlReader.Create("CrazyXmlFile",settings);
Here is a complete sample:
Imports
System.Xml
Imports System.Xml.Schema
Module Module1
Sub Main(ByVal args As String())
Dim rs As XmlReaderSettings = New XmlReaderSettings()
rs.ValidationType = ValidationType.Schema
rs.Schemas.Add(Nothing, args(1))
AddHandler rs.ValidationEventHandler, AddressOf HandleValidationError
Dim r As XmlReader = XmlReader.Create(args(0), rs)
Using (r)
While (r.Read())
Console.WriteLine(r.NodeType.ToString() + ": " + r.Name)
If (r.SchemaInfo Is Nothing) Then
Console.WriteLine(" no schema information")
End If
End While
End Using
End Sub
Sub HandleValidationError(ByVal sender As Object, ByVal e As ValidationEventArgs)
Console.WriteLine(e.Severity.ToString() + ": " + e.Message)
End Sub End
Module Hi
Can you please give me a layout (How to do) for the following? also I am new for this language.
I have the following:
XML schema, and Access database.
I am trying to do the following. Using c#
1.Create a Windows forms ( includes subforms and arround 10 tabs)
2. Fields bound to the tables in the database
3.When user entered the data and click the button, the data should be validated against the schema.If the data is not valid then the user must be informed by message and directed to the relevant field in the form. (Please let me know any other effective way to do it without using schema).
4. If the data is valid then shoud be save as xml file with a autonumber from the table as file name and the data should be saved to the database as well.
Please give me head start How I can best implement this.
Many thanks in advance
Vaish