Validating an XML File using XSD in .NET .20

Recently I had an issue with .NET 2.0 XML validation. Lets see whats the difference in .NET 2.0 from .NET 1.1 

Below given is the.NET 1.1 code for validating an XML File using an XSD File.

.NET 1.1 Code

//.NET1.1 code
///
/// Methode to validate XML File///
///

This method expects input XML as string
///
Path to schema file
/// true if xml is validated else false
private bool ValidateXmlUsingXsd(string XmlData,String SchemaPath)
{
XmlValidatingReader v = new XmlValidatingReader(XmlData, XmlNodeType.Document, null);
v.ValidationType = ValidationType.Schema;
v.ValidationEventHandler +=
new ValidationEventHandler(MyValidationEventHandler); 
 

while (v.Read())
{
// Can add code here to process the content.
}
v.Close();

return isValid;

}

///

public static void MyValidationEventHandler(object sender,
ValidationEventArgs args)
{
//these two variables should be initialized as class level variables
isValid = false;
errorMessage = “Validation eventn” + args.Message;///
/// Method to get XML in a string from an XML file
///
///

}

///
private string GetStringFromXML(string fileName)
{
StreamReader rd = new StreamReader(fileName);
string str = rd.ReadToEnd();
rd.Close();
return str;
}

Calling the method :
bool valid = ValidateXmlUsingXsd(str, txtXSD.Text);

 

There are some changes in the .NET 2.0 code for XML Validation .
XmlValidatingReader is marked as obsolete. Need to use XMLReader.Create() using XmlReaderSettngs instead

There are some behavioral changes between validation using the XmlReaderSettings and XmlSchemaSet classes and validation using the XmlValidatingReader class.

The XmlReaderSettings and XmlSchemaSet classes do not support XML-Data Reduced (XDR) schema validation.

The most important difference I found out is that to do XML data validation using a schema, settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
Flag must be enabled. Otherwise the Schema check error will not be displayed.

//.NET2.0 code
private bool ValidateXmlUsingXsd2(string XmlData,String SchemaPath)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, SchemaPath);
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
More about XmlSchemaValidationFlags Enumeration

StringReader xmlStream = new StringReader(XmlData);
XmlReader reader = XmlReader.Create(xmlStream, settings);
while (reader.Read()) ;

return isValid;
}

private static void ValidationCallBack(object sender, ValidationEventArgs e)
{
isValid = false;
errorMessage = “Validation Error: ” + e.Message;
}

 

Member name
Description
AllowXmlAttributes
Allow xml:* attributes even if they are not defined in the schema. The attributes will be validated based on their data type.
None
Do not process identity constraints, inline schemas, schema location hints, or report schema validation warnings.
ProcessIdentityConstraints
Process identity constraints (xs:ID, xs:IDREF, xs:key, xs:keyref, xs:unique) encountered during validation.
ProcessInlineSchema
Process inline schemas encountered during validation.
ProcessSchemaLocation
Process schema location hints (xsi:schemaLocation, xsi:noNamespaceSchemaLocation) encountered during validation.
ReportValidationWarnings
Report schema validation warnings encountered during validation.

In a nutshell, Always set settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; when a schema validation is required for the XML in .NET 2.0

8 comments

When is the next post comming on this topic.

Regards

Viki

Success is not measured by what you accomplish but by the opposition you have encountered, and the courage with which you have maintained the struggle against overwhelming odds.

I agree that the 2 income family was a faustian bargain. It takes a lot to run a household and raise kids. Doing so on top of a fulltime job runs you into the ground

i was looking for crucial information on this subject. the information was important as i am about to launch my own portal. thanks for providing a missing link in my business.

i love dancing. your post is very usefull. i have a gaming site. i wish you continued success with your site

Oh my goodness! an incredible article dude. Thank you However I’m experiencing subject with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting equivalent rss problem? Anybody who is aware of kindly respond. Thnkx

sometimes some customers have information listed with a directory listing service. and you may be able to find the number via a search engine. another way of course, is to call up the number and ask for the info if the number can get you to a live person.

Leave a reply