Reviews US Money Reserve research paper Motivational Quotes  ServicesListed.com Free cell phones   Custom Writing
Try the best slots i found online, you will love it!
Nishanth's Blog | Validating an XML File using XSD in .NET .20

Validating an XML File using XSD in .NET .20

by Nishanth 2. November 2008 07:04

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 event\n” + 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

Comments

10/4/2011 11:42:11 PM #

I genuinely have looking at on this texture scene , it encloses sizzling notifys .

Dino Borowiak France

11/15/2011 8:47:04 AM #

Rattling pleasing proposal moreover worthy submissive equipment , truly slight otherwise we want : D.

immobilier maroc 20 France

11/17/2011 2:55:18 AM #

I was looking at quantity of your blog advises on this internet place plus I comprehend this tangle place is rattling instructive! Contain posting .

Marrakech France

12/18/2011 2:05:46 PM #

my personal blogroll.

admin

12/18/2011 10:17:10 PM #

This .net code is really awesome. I am so happy that you took the time sharing this to us. You're a great help indeed!

weight loss foods United States

1/6/2012 8:52:17 PM #

What an awesome idea. I love how beautiful the wine box makes the garden look You have such great ideas for up cycling stuff

the lesbian sex archives United States

1/10/2012 9:29:10 AM #

Hi all the code you need is below, just copy it to where u want it to show.

Maurice Roberta United States

1/11/2012 4:20:11 PM #

We have personally tested the crack and code and its working. Please check it again.

Pearly Bundage United States

1/25/2012 1:59:38 PM #

Good query tips. It's so much easier to mess up a query than to get it right.

chinese news United States

1/26/2012 6:45:10 PM #

Hello I am so glad I found your blog, I really found you by mistake, while I was browsing on Yahoo for something else, Regardless I am here now and would just like to say thank you for a incredible post and a all round thrilling blog (I also love the theme/design), I don't have time to go through it all at the moment but I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the excellent job.

Valene Starnes United States

2/1/2012 1:03:02 AM #

Thats really cool im gona try it! Your so pretttty!

Ira Sandstrom United States

Add comment




  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen


About Me

Nishanth Nair  

 A die hard fan of Rise of Nations... I write few softwares for McAfee Inc. for a living.

 

Portfolio:

http://morningbellnews.com
TheXorb.com
http://Necab.org
http://SufiKatha.com
http://DeepaRecipes.com
http://ManipalFoundation.in
http://NetraArts.com
http‌://EnWebTech.com 

http://homeopathyinchicago.com/


Contact : Nair.Nishanth@Gmail.com 

 

All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer.

Tag cloud

free counters