invalid xml doesn't throw error

A

agda.karlberg

Hi,

Apologies if this is a really stupid question, I haven't done much xml
before.

I need to validate an xml file against a schema, but when I try to
validate an invalid file it doesn't throw an error.

Here's my C# code:

namespace MyValidationTool
{
class Program
{
static void Main(string[] args)
{
string xmlFileName = "thexmlfile.xml";
string schemaFileName = "theschemafile.xsd";

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;

XmlSchemaSet schemas = new XmlSchemaSet();
settings.Schemas = schemas;

schemas.Add(null, schemaFileName);

settings.ValidationEventHandler += new
ValidationEventHandler(settings_ValidationEventHandler);

XmlReader validator = XmlReader.Create(xmlFileName, settings);

try
{
while (validator.Read())
{
Console.WriteLine("test");
Console.ReadLine();
}
}
catch (XmlException err)
{
Console.WriteLine(err.Message);
Console.ReadLine();
}
finally
{
validator.Close();
}
}

static void settings_ValidationEventHandler(object sender,
ValidationEventArgs e)
{
Console.WriteLine("Validation error: " + e.Message);
Console.ReadLine();
}
}
}


thexmlfile:
<?xml version="1.0" encoding="UTF-8"?>

<note xmlns="http://www.w3schools.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://www.w3schools.com theschemafile.xsd">

<to>Me</to>
<from>Myself</from>
<heading>Does it work?</heading>
<body>Nope, unfortunately not.</body>
</note>


theschemafile:
<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://
www.w3schools.com" targetNamespace="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


As I said, if I try to break it by making the xml file invalid it
still doesn't throw an error.

I'm presuming I'm missing something really obvious here, so I'd really
appreciate if anoyone who knows why it's happening could please give
me a pointer in the right direction.

Thanks,

AK
 
J

Jon Skeet [C# MVP]

As I said, if I try to break it by making the xml file invalid it
still doesn't throw an error.

Please give an example of an invalid file, so we can reproduce the
problem. Otherwise the issue could very easily be your notion of an
invalid file instead of the code actually being wrong.
 
M

Martin Honnen

M

Martin Honnen

Martin said:
What exactly did you try to make the file invalid? Show us an XML sample
that you think is invalid and should cause the ValidationEventHandler to
fire.

For instance when I change your XML to insert an element not defined in
your schema

<note xmlns="http://www.w3schools.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://www.w3schools.com theschemafile.xsd">

<to>Me</to>
<from>Myself</from>
<heading>Does it work?</heading>
<body>Nope, unfortunately not.</body>
<foo/>
</note>

then your code correctly reports that:

"Validation error: The element 'note' in namespace
'http://www.w3schools.com' has
invalid child element 'foo' in namespace 'http://www.w3schools.com'."
 
A

agda.karlberg

Turns out it's my debugging skills that was the problem! This is also
the first time I'm doing a console application, and as I'm writing out
the below it stops there and doesn't go into the catch block:

while (validator.Read())
{
Console.WriteLine("test");
Console.ReadLine();
}

If I remove this it throws an error when expected (I broke it by
removing > from one of the tags as well as making one of the elements
into an int rather than a string, will provide examples next time I
ask something).

Thanks for taking the time to look at this, sorry it was only me being
silly!

AK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top