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 MyValidationToo l
{
class Program
{
static void Main(string[] args)
{
string xmlFileName = "thexmlfile.xml ";
string schemaFileName = "theschemafile. xsd";
XmlReaderSettin gs settings = new XmlReaderSettin gs();
settings.Valida tionType = ValidationType. Schema;
XmlSchemaSet schemas = new XmlSchemaSet();
settings.Schema s = schemas;
schemas.Add(nul l, schemaFileName) ;
settings.Valida tionEventHandle r += new
ValidationEvent Handler(setting s_ValidationEve ntHandler);
XmlReader validator = XmlReader.Creat e(xmlFileName, settings);
try
{
while (validator.Read ())
{
Console.WriteLi ne("test");
Console.ReadLin e();
}
}
catch (XmlException err)
{
Console.WriteLi ne(err.Message) ;
Console.ReadLin e();
}
finally
{
validator.Close ();
}
}
static void settings_Valida tionEventHandle r(object sender,
ValidationEvent Args e)
{
Console.WriteLi ne("Validation error: " + e.Message);
Console.ReadLin e();
}
}
}
thexmlfile:
<?xml version="1.0" encoding="UTF-8"?>
<note xmlns="http://www.w3schools.c om"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocati on="http://www.w3schools.c om theschemafile.x sd">
<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.c om" targetNamespace ="http://www.w3schools.c om"
elementFormDefa ult="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
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 MyValidationToo l
{
class Program
{
static void Main(string[] args)
{
string xmlFileName = "thexmlfile.xml ";
string schemaFileName = "theschemafile. xsd";
XmlReaderSettin gs settings = new XmlReaderSettin gs();
settings.Valida tionType = ValidationType. Schema;
XmlSchemaSet schemas = new XmlSchemaSet();
settings.Schema s = schemas;
schemas.Add(nul l, schemaFileName) ;
settings.Valida tionEventHandle r += new
ValidationEvent Handler(setting s_ValidationEve ntHandler);
XmlReader validator = XmlReader.Creat e(xmlFileName, settings);
try
{
while (validator.Read ())
{
Console.WriteLi ne("test");
Console.ReadLin e();
}
}
catch (XmlException err)
{
Console.WriteLi ne(err.Message) ;
Console.ReadLin e();
}
finally
{
validator.Close ();
}
}
static void settings_Valida tionEventHandle r(object sender,
ValidationEvent Args e)
{
Console.WriteLi ne("Validation error: " + e.Message);
Console.ReadLin e();
}
}
}
thexmlfile:
<?xml version="1.0" encoding="UTF-8"?>
<note xmlns="http://www.w3schools.c om"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocati on="http://www.w3schools.c om theschemafile.x sd">
<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.c om" targetNamespace ="http://www.w3schools.c om"
elementFormDefa ult="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
Comment