C# XML validation against XSD

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<div style="color:#000000; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:67%; background-color:#ffffff; margin:8px
Hi All,
I am validating an xml markup against a schema using a XmlReader object using "Windows application". To find validation warnings i am passing a schemavalidation settings object. How do I find the elements that are causing the validation errors and their
value that is causing the validation errors.<br/>
Actually i have to generate a report of all such validation errors.
This report needs to capture the following entities.
1.Name of the element causing the error .
2.Type of the error.
3.Value of the element .
Any Suggestions.Quick replys are appreciable.Since i was struck up at this stage.
MY Code :
public void settings_ValidationEventHandler(object sender, ValidationEventArgs args)<br/>
{<br/>
<br/>
ErrorMessage = ErrorMessage + args.Message ;<br/>
ErrorsCount++;<br/>
<br/>
<br/>
}
<span style="color:#0000ff <span style="font-family:; color:#0000ff
public

<span style="color:#0000ff bool SValidate(<span style="color:#0000ff string xmlFile)
{
<span style="color:#0000ff bool result = <span style="color:#0000ff false;
<span style="color:#2b91af XmlReader xmlreader = <span style="color:#0000ff null;
<span style="color:#2b91af FileStream fs = <span style="color:#0000ff null;
<span style="color:#2b91af XmlReaderSettings settings = <span style="color:#0000ff null;
<span style="color:#2b91af XmlSchemaSet set = <span style="color:#0000ff null;
<span style="color:#2b91af XmlReader schemaReader = <span style="color:#0000ff null;
<span style="color:#0000ff string schemaFile = <span style="color:#a31515 XSDPath;
<span style="color:#0000ff try
{
fs = <span style="color:#0000ff new <span style="color:#2b91af FileStream(xmlFile, <span style="color:#2b91af FileMode.Open);
settings = <span style="color:#0000ff new <span style="color:#2b91af XmlReaderSettings();
settings.IgnoreWhitespace = <span style="color:#0000ff true;
settings.IgnoreComments = <span style="color:#0000ff true;
settings.ValidationType = <span style="color:#2b91af ValidationType.Schema;
settings.ValidationFlags = <span style="color:#2b91af XmlSchemaValidationFlags.ReportValidationWarnings;
set = <span style="color:#0000ff new <span style="color:#2b91af XmlSchemaSet();
<span style="color:#2b91af TextReader reader = <span style="color:#0000ff new <span style="color:#2b91af StreamReader(schemaFile);
schemaReader = <span style="color:#2b91af XmlReader.Create(reader);
set.Add(<span style="color:#0000ff null, schemaReader);
settings.Schemas.Add(set);
settings.Schemas.Compile();
settings.ValidationEventHandler += <span style="color:#0000ff new <span style="color:#2b91af ValidationEventHandler(settings_ValidationEventHandler);
xmlreader = <span style="color:#2b91af XmlTextReader.Create(fs, settings);
<span style="color:#0000ff while (xmlreader.Read()) ;
<span style="color:#0000ff if (ErrorsCount == 0)
{

}
<span style="color:#0000ff else
{
}
}
<span style="color:#0000ff catch (<span style="color:#2b91af Exception ex)
{
result = <span style="color:#0000ff false;
<span style="color:#2b91af MessageBox.Show(ex.Message);

}
<span style="color:#0000ff return result;
}
Thanks& Regards,<br/>
Krishna
<
Thanks & Regards, Krishna <hr class="sig Thanks & Regards, Krishna

View the full article
 
Back
Top