brdrok
asked on
Need help validating XML against XSD schema.
Hi,
XML and XSD newbie here. I am trying to validate an XML file with an XSD schema but even when I introduce errors on purpose, I still do not get an validation error.
Below is my XSD code:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="FlatFileGenXMLSchema"
targetNamespace="http://tempuri.org/FlatFileGenXMLSchema.xsd"
elementFormDefault="qualif ied"
xmlns="http://tempuri.org/FlatFileGenXMLSchema.xsd"
xmlns:mstns="http://tempuri.org/FlatFileGenXMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="DataSources" type="dataSourcesType"></x s:element>
<xs:complexType name="dataSourcesType">
<xs:sequence>
<xs:element name="ZipGroup" type="zipGroupType"></xs:e lement>
</xs:sequence>
<xs:attribute name="continueOnError" type="xs:boolean" />
</xs:complexType>
<xs:complexType name="zipGroupType">
<xs:sequence>
<xs:element name="Summary" type="summaryType" />
<xs:element name="DataSource" type="dataSourceType" />
</xs:sequence>
<xs:attribute name="FileName" type="xs:string" use="required" />
<xs:attribute name="Destination" type="xs:string" use="required" />
<xs:attribute name="ToZip" type="xs:string" use="required" />
<xs:attribute name="intHour" type="xs:int" use="required" />
</xs:complexType>
etc...
Below is an excerpt of my XML file:
<?xml version='1.0' encoding='utf-8'?>
<DataSources continueOnError="false">
<ZipGroup FileName="ZipGroup1" Destination="C:\FlatFile" ToZip="true" intHour="lalalal">
<DataSource>
<FileName>Person.txt</File Name>
<Destination>C:\FlatFile</ Destinatio n>
<SQLQuery>SELECT * FROM PERSONS</SQLQuery>
<ConnectionString>server=. ;uid=xx;pw d=xxxxxxx; database=x xxxxx;</Co nnectionSt ring>
<Factory>System.Data.SqlCl ient</Fact ory>
<Encoding>Unicode</Encodin g>
<AppendDateToFileName>true </AppendDa teToFileNa me>
</DataSource>
Below is my C# code that is suppose to validate my XML file against the XSD schema:
private void button1_Click(object sender, EventArgs e)
{
string xsdFile = @"C:\mySchema.xsd";
string xmlFile = @"C:\myXML.xml";
XmlReader vr;
//validate XML Schema itself
ValidationEventHandler handler = new ValidationEventHandler(For m1.Handler );
XmlSchema schema = XmlSchema.Read(File.OpenRe ad(xsdFile ), handler);
schema.Compile(handler);
//validate XML against the Schema
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://tempuri.org/FlatFileGenXMLSchema.xsd", xsdFile);
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHa ndler += new ValidationEventHandler(set tings_Vali dationEven tHandler);
FileStream fs = new FileStream(xmlFile, FileMode.Open);
vr = XmlReader.Create(fs, settings);
while (vr.Read())
{
//do nothing
}
vr.Close();
MessageBox.Show("Doc is valid");
Please notice that I set the values of "intHour" to "lalalala" for the <ZipGroup> element. In my XSD, I have specified that the "intHour" attribute is suppose to contain only integers.
Thanks
XML and XSD newbie here. I am trying to validate an XML file with an XSD schema but even when I introduce errors on purpose, I still do not get an validation error.
Below is my XSD code:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="FlatFileGenXMLSchema"
targetNamespace="http://tempuri.org/FlatFileGenXMLSchema.xsd"
elementFormDefault="qualif
xmlns="http://tempuri.org/FlatFileGenXMLSchema.xsd"
xmlns:mstns="http://tempuri.org/FlatFileGenXMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="DataSources" type="dataSourcesType"></x
<xs:complexType name="dataSourcesType">
<xs:sequence>
<xs:element name="ZipGroup" type="zipGroupType"></xs:e
</xs:sequence>
<xs:attribute name="continueOnError" type="xs:boolean" />
</xs:complexType>
<xs:complexType name="zipGroupType">
<xs:sequence>
<xs:element name="Summary" type="summaryType" />
<xs:element name="DataSource" type="dataSourceType" />
</xs:sequence>
<xs:attribute name="FileName" type="xs:string" use="required" />
<xs:attribute name="Destination" type="xs:string" use="required" />
<xs:attribute name="ToZip" type="xs:string" use="required" />
<xs:attribute name="intHour" type="xs:int" use="required" />
</xs:complexType>
etc...
Below is an excerpt of my XML file:
<?xml version='1.0' encoding='utf-8'?>
<DataSources continueOnError="false">
<ZipGroup FileName="ZipGroup1" Destination="C:\FlatFile" ToZip="true" intHour="lalalal">
<DataSource>
<FileName>Person.txt</File
<Destination>C:\FlatFile</
<SQLQuery>SELECT * FROM PERSONS</SQLQuery>
<ConnectionString>server=.
<Factory>System.Data.SqlCl
<Encoding>Unicode</Encodin
<AppendDateToFileName>true
</DataSource>
Below is my C# code that is suppose to validate my XML file against the XSD schema:
private void button1_Click(object sender, EventArgs e)
{
string xsdFile = @"C:\mySchema.xsd";
string xmlFile = @"C:\myXML.xml";
XmlReader vr;
//validate XML Schema itself
ValidationEventHandler handler = new ValidationEventHandler(For
XmlSchema schema = XmlSchema.Read(File.OpenRe
schema.Compile(handler);
//validate XML against the Schema
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://tempuri.org/FlatFileGenXMLSchema.xsd", xsdFile);
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHa
FileStream fs = new FileStream(xmlFile, FileMode.Open);
vr = XmlReader.Create(fs, settings);
while (vr.Read())
{
//do nothing
}
vr.Close();
MessageBox.Show("Doc is valid");
Please notice that I set the values of "intHour" to "lalalala" for the <ZipGroup> element. In my XSD, I have specified that the "intHour" attribute is suppose to contain only integers.
Thanks
Where's the eventHandler code? I am assuming you just forgot to paste that in as well...
ASKER
The event handlers are there. I didn't want to totally clutter up the screen with code.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you cool_bike,
in the end it turned out that the attributes of my <xs:schema...> elements were a little out of whack.
I simplified it to the following:
<xs:schema id="FlatFileGenXMLSchema" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas- microsoft- com:xml-ms data">
and changed the following line of code from:
settings.Schemas.Add("http://tempuri.org/FlatFileGenXMLSchema.xsd", xsdFile);
to
settings.Schemas.Add("", xsdFile);
I don't know how or why it works, but at this point, I am just glad it works =)
By the way Cool Bike Rider, I am curious why you have wrapped your validation logic inside a Try-Catch statement because you already have the following line of code:
vr.ValidationEventHandler += new ValidationEventHandler(Val idationHan dler);
if I understand correctly the event handler will catch all the invalid xml elements.
in the end it turned out that the attributes of my <xs:schema...> elements were a little out of whack.
I simplified it to the following:
<xs:schema id="FlatFileGenXMLSchema" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-
and changed the following line of code from:
settings.Schemas.Add("http://tempuri.org/FlatFileGenXMLSchema.xsd", xsdFile);
to
settings.Schemas.Add("", xsdFile);
I don't know how or why it works, but at this point, I am just glad it works =)
By the way Cool Bike Rider, I am curious why you have wrapped your validation logic inside a Try-Catch statement because you already have the following line of code:
vr.ValidationEventHandler += new ValidationEventHandler(Val
if I understand correctly the event handler will catch all the invalid xml elements.
ASKER
nevermind...the
if (ErrorsCount > 0)
{
throw new Exception(ErrorMessage);
}
lines somehow didn't register in my head.
if (ErrorsCount > 0)
{
throw new Exception(ErrorMessage);
}
lines somehow didn't register in my head.
Following is my event handler... here 'ErrorMessage' and 'ErrorsCount' are static variables.... same I am using inside validation function.
public static void ValidationHandler(object sender, ValidationEventArgs args)
{
ErrorMessage = ErrorMessage + args.Message + "\r\n";
ErrorsCount ++;
}
I just tried a different logic here... Hope you convinced... :)
public static void ValidationHandler(object sender, ValidationEventArgs args)
{
ErrorMessage = ErrorMessage + args.Message + "\r\n";
ErrorsCount ++;
}
I just tried a different logic here... Hope you convinced... :)