473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

invalid xml doesn't throw error

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_ValidationEventHan dler);

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
Sep 24 '08 #1
4 1406
<ag***********@gmail.comwrote:

<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 24 '08 #2
ag***********@gmail.com wrote:
As I said, if I try to break it by making the xml file invalid it
still doesn't throw an error.
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.
The code you have looks ok to me, only you might want to set
ValidationFlags to ReportValidationWarnings as well, see
http://blogs.msdn.com/xmlteam/archiv...-to-trust.aspx
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 24 '08 #3
Martin Honnen wrote:
ag***********@gmail.com wrote:
>As I said, if I try to break it by making the xml file invalid it
still doesn't throw an error.

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'."

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 24 '08 #4
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
Sep 25 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
0
by: William Stacey [MVP] | last post by:
Had a method that got some string info from mp3 tags in N files and serializes this class and deserializes at other side. Works ok except sometimes get chars that choke the XmlSerializer. After...
1
by: gane kol | last post by:
Hi I am using DES algorithm. I am getting an error message in a few cases of the querystring Error Message : Length of the data to decrypt is invalid. Error Method : System.String...
15
by: David | last post by:
Hi, I have built a web application that will be a very high profile application. We had tested it, demonstrated it and shown that it all works. On a dress rehearsal run through, it failed...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
3
by: Ajay Choudhary | last post by:
Hi, If the client tries to access a web service with invalid namespace, it gets a SOAP exception as invalid SOAPAction. The invalid namespace will happen because the proxies on the client side...
25
by: dennijr | last post by:
ok, shell always used to be easy for me, now its starting to get annoying cause i dont know wats wrong heres the simplist code possible: Private Sub IExplorer_Click() a = Shell("C:\Program...
2
by: eruss21 | last post by:
Here's my code. Don't know what's going on. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.