bovlk
asked on
XML Schema validation
Hello,
I've got the code below in ASP.NET 3.5/C#. I pass it a schema file and an XML that does not complain to the XSD at all, yet I don't get the exception. I spent about an hour trying to figure out what's wrong, yet did not come to a solution. Any help is welcome. Thanks
Bolek
I've got the code below in ASP.NET 3.5/C#. I pass it a schema file and an XML that does not complain to the XSD at all, yet I don't get the exception. I spent about an hour trying to figure out what's wrong, yet did not come to a solution. Any help is welcome. Thanks
Bolek
XmlSchemaSet schemas = new XmlSchemaSet();
using (FileStream fs = new FileStream( "schema.xsd"), FileMode.Open, FileAccess.Read))
using (XmlReader str = XmlReader.Create(fs))
{
XmlSchemaAvenue.Add(null, str);
}
try
{
string xml = ReadXmlFromZipFile( "file.xml");
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ValidationType = ValidationType.Schema;
xrs.Schemas = schema;
using (StringReader sr = new StringReader(xml))
using (XmlReader reader = XmlReader.Create(sr, xrs))
{
while (reader.Read()) ;
}
}
catch (XmlSchemaValidationException)
{
Error();
}
The handler xrs_ValidationEventHandler will be executed for every validation error the parser encounters.
ASKER
Does not work either.
ASKER
The handler does not get called at all.
Did you add line 17 from my post as well?
ASKER
Yes.
Are you sure your instance document is invalid? The handler fired for me. The handler only fires when an error is encountered. If the document is valid, the handler will never fire.
Here is the code I used as well as my test documents.
test.xsd
static void Main(string[] args)
{
(new Program()).Test();
}
System.Data.SqlClient.SqlDataReader reader;
public void Test()
{
XmlSchemaSet schemas = new XmlSchemaSet();
using (FileStream fs = new FileStream("test.xsd", FileMode.Open, FileAccess.Read))
{
using (XmlReader str = XmlReader.Create(fs))
{
schemas.Add(null, str);
}
}
try
{
string xml = (new StreamReader("test.xml").ReadToEnd());
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ValidationType = ValidationType.Schema;
xrs.ValidationEventHandler += new ValidationEventHandler(xrs_ValidationEventHandler);
xrs.Schemas = schemas;
using (StringReader sr = new StringReader(xml))
using (XmlReader reader = XmlReader.Create(sr, xrs))
{
while (reader.Read()) ;
}
}
catch (XmlSchemaValidationException)
{
System.Diagnostics.Debugger.Break();
}
}
void xrs_ValidationEventHandler(object sender, ValidationEventArgs e)
{
System.Diagnostics.Debugger.Break();
}
test.xmltest.xsd
Also, ValidationEventArgs in the handler has a "Message" property which will tell you why validation failed.
ASKER
OK, your examples work fine. However, if I try the file below, which has even the root element different, it goes without noticing the problem.
ASKER
Here's the file - I forgot to attach it.
workbook.xml
workbook.xml
I'm sorry, but what is different about the root? I see:
<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
</workbook>
<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
</workbook>
ASKER
Your XML and XSD have <shiporder> as root, mine has <workbook>. Shouldn't it be detected as schema violation? Obviously, a file with different name of the root node cannot conform to the schema.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK, so is there any way of validating so that if the document has different root and/or namespace, I always get an error, and if it has the same root & namespace, it is validated by the XSD? Of course I can simply find the root node and do the comparison manually, but is there any other way, possibly using the XSD file and XmlReader again?
Open in new window