Navigation

Search

Categories

On this page

Enforcing Xml Schema Validation

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 83
This Year: 0
This Month: 0
This Week: 0
Comments: 20

Sign In

# Sunday, April 25, 2004
Enforcing Xml Schema Validation
Sunday, April 25, 2004 3:26:08 PM UTC ( General )

Validating xml documents is one of the more common tasks when building extensibility points, integration infrastructure or enabling configuration support. The .Net Framework provides a type called XmlValidatingReader to help you with this task. There are however a few pitfalls you should be aware of if you want to be certain that an xml instance conforms to the provided xml schema.

Always Define a Validation Event Handler

The xml validation system uses severity levels to indicate the level of validation failure. There are two severity levels available; namely XmlSeverityType.Error and XmlSeverityType.Warning. The interesting thing is that unless you specify your own validation handler only the severity level of XmlSeverityType.Error will cause an exception to be thrown; all warnings are ignored.

This may not seem like a big issue until you realize that “missing schema for provided type” is a warning and not an error. Therefore, unless you have a schema loaded for the xml instance namespace, anything goes.

When you provide your own validation handler, you can intercept both severity levels and act accordingly. A simple default could be to throw the exception provided within the ValidationEventArgs parameter, as you will see in the sample below.

Always Specify a ValidationType

The default ValidationType is ValidationType.Auto, which means that unless you have provided a schema for the instance namespace, it will assume ValidationType.None and no validation takes place.

If you are using Xml Schemas, you should set this to ValidationType.Schema.

In Conclusion

The common theme here is that vanilla validation only works if you happen to have the same namespace in your xml instance and xml schema documents. Unless your application explicitly checks the namespace of the xml instances it processes, you could easily submit whatever xml document you wanted as long as it uses a different namespace than what the schema expects.

You will find a code snippet below that incorporates this advice, and hopefully gives you some ideas on how to address these issues in you code.

Code Snippet

static void ValidationHandler(object o, ValidationEventArgs args)
{
 throw args.Exception;
}

static void Validate(XmlReader reader, XmlSchema schema)
{
 XmlValidatingReader validatingReader = new XmlValidatingReader(reader);
 validatingReader.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);
 validatingReader.Schemas.Add(schema);
 validatingReader.ValidationType = ValidationType.Schema;
 while (validatingReader.Read());
 validatingReader.Close();
}
Comments [0] | | # 
Related posts:
A revived browser race – the web application is not dead
Gtk# and Glade# VS.NET 2003 SDK
Mono 1.0 Hits RTM
Work, WSE and Indigo
PDC is on!
Melbourne, Australia
Tracked by:
"http://cost.20xhost.com" (http://cost.20xhost.com) [Pingback]
"http://freewebs.com/lcddlp/33/sitemap17.html" (http://freewebs.com/lcddlp/33/si... [Pingback]
"http://no938076.ki6dgrr.info/sitemap20.html" (http://no938076.ki6dgrr.info/site... [Pingback]
"http://ty9432.mi4rdqf.info/sitemap1.html" (http://ty9432.mi4rdqf.info/sitemap1.... [Pingback]
"http://bbbbbbbbbbcc.net" (http://bbbbbbbbbbcc.net) [Pingback]
 [Pingback]
"http://thebix.com/includes/compat/pages/26874149/index.html" (http://thebix.com... [Pingback]
"http://add2rss.com/img/design/pages/13916519/online-viagra-sales.html" (http://... [Pingback]
"http://plantmol.com/pages/62472510/viagra-medical.html" (http://plantmol.com/pa... [Pingback]
"http://plantmol.com/pages/33013774/can-i-buy-viagra-in-britain.html" (http://pl... [Pingback]
"http://ncdtnanotechportal.info/generator/pages/88274118/birmingham-viagra-sampl... [Pingback]
"http://pddownloads.com/pages/88025996/index.html" (http://pddownloads.com/pages... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/pages/97941190/women-takin... [Pingback]
"http://discussgod.com/cpstyles/pages/67562704/gahanna-ohio-picture-with-the-eas... [Pingback]
"http://plantmol.com/pages/62472510/viagra-liver-problems.html" (http://plantmol... [Pingback]
"http://vladan.strigo.net/wp-includes/js/pages/33747906/viagra-patent-china.html... [Pingback]
"http://coolioness.com/attachments/pages/attachments/pages/53101220/gay-men-test... [Pingback]
"http://jivest2006.com/pages/95314742/bravoop-100-funniest-movies.html" (http://... [Pingback]
"http://ziaeisoft.com/db/pages/55191207/top-50-tearjerker-movies.html" (http://z... [Pingback]
"http://coolioness.com/attachments/pages/attachments/pages/99012219/cozumel-obta... [Pingback]
"http://hrvatska.biz/wp-includes/js/pages/78020075/camp-in-new-zealand-for-girls... [Pingback]
"http://pddownloads.com/pages/63534267/free-nude-pics-of-tiffiny-falon.html" (ht... [Pingback]
"http://slaterjohn.com/downloads/2col/53744671/first-hand-cialis-experience.html... [Pingback]
"http://witze-humor.de/templates/images/pages/templates/images/pages/78175370/vi... [Pingback]
"http://pddownloads.com/pages/86610121/herbal-alternatives-for-viagra.html" (htt... [Pingback]
"http://entartistes.ca/images/images/pages/74678886/buy-generic-viagra.html" (ht... [Pingback]
"http://split-dalmatia.com/split-dalmatia.com/images/pages/48571052/viagra-colum... [Pingback]
"http://slaterjohn.com/downloads/2col/16835960/index.html" (http://slaterjohn.co... [Pingback]
"http://split-dalmatia.com/split-dalmatia.com/images/pages/81160650/counterfeit-... [Pingback]
"http://jivest2006.com/pages/59888246/viagra-generic-india-complaints.html" (htt... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/pages/09113833/index.html"... [Pingback]
"http://thebix.com/includes/compat/pages/41528949/index.html" (http://thebix.com... [Pingback]
"http://split-dalmatia.com/split-dalmatia.com/images/pages/52987785/filmy-viagra... [Pingback]
"http://ziaeisoft.com/db/pages/15928272/index.html" (http://ziaeisoft.com/db/pag... [Pingback]
"http://entartistes.ca/images/images/pages/25903774/50-mg-viagra.html" (http://e... [Pingback]
"http://add2rss.com/img/design/pages/35295115/canadian-pharmacy-cialis.html" (ht... [Pingback]
"http://blog.netmedia.hr/wp-includes/js/pages/wp-includes/js/pages/73143969/viag... [Pingback]
"http://restablog.com/motorcycles/sitemap1.html" (http://restablog.com/motorcycl... [Pingback]
"http://discussgod.com/cpstyles/docs/62161481/index.html" (http://discussgod.com... [Pingback]
"http://coolioness.com/attachments/docs/03698289/index.html" (http://coolioness.... [Pingback]
"http://lecouac.org/ecrire/lang/docs/30125734/index.html" (http://lecouac.org/ec... [Pingback]
"http://witze-humor.de/templates/images/docs/83157240/index.html" (http://witze-... [Pingback]
"http://lecouac.org/ecrire/lang/docs/20007231/index.html" (http://lecouac.org/ec... [Pingback]
"http://lecouac.org/ecrire/lang/docs/49649526/index.html" (http://lecouac.org/ec... [Pingback]
"http://coolioness.com/attachments/docs/75395149/index.html" (http://coolioness.... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/70471394/index.html" ... [Pingback]
"http://thejohnslater.com/pix/img/docs/73486930/index.html" (http://thejohnslate... [Pingback]
"http://discussgod.com/cpstyles/docs/25383456/index.html" (http://discussgod.com... [Pingback]
"http://discussgod.com/cpstyles/docs/90092602/index.html" (http://discussgod.com... [Pingback]
"http://swellhead.netswellhead.net/docs/84545083/index.html" (http://swellhead.n... [Pingback]
"http://thebix.com/includes/compat/docs/15132509/index.html" (http://thebix.com/... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/48335156/index.html" ... [Pingback]
"http://plantmol.com/docs/99021843/index.html" (http://plantmol.com/docs/9902184... [Pingback]
"http://legambitdufou.org/Library/docs/28049195/index.html" (http://legambitdufo... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/87090382/index.h... [Pingback]
"http://martinrozon.com/images/photos/docs/54373182/index.html" (http://martinro... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/60974094/index.h... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/54089144/index.h... [Pingback]
"http://coolioness.com/attachments/docs/83777724/index.html" (http://coolioness.... [Pingback]
"http://temerav.com/images/menu/91084644/index.html" (http://temerav.com/images/... [Pingback]
"http://entartistes.ca/images/images/docs/28212733/index.html" (http://entartist... [Pingback]
"http://temerav.com/images/menu/20420171/index.html" (http://temerav.com/images/... [Pingback]
"http://temerav.com/images/menu/05559064/index.html" (http://temerav.com/images/... [Pingback]
"http://vladan.strigo.net/wp-includes/js/docs/25746442/index.html" (http://vlada... [Pingback]
"http://pspdesktops.com/fileupload/store/docs/18769945/index.html" (http://pspde... [Pingback]
"http://realestate.hr/templates/css/docs/36157459/index.html" (http://realestate... [Pingback]
"http://plantmol.com/docs/60217277/index.html" (http://plantmol.com/docs/6021727... [Pingback]
"http://pspdesktops.com/fileupload/store/docs/04061117/index.html" (http://pspde... [Pingback]
"http://jivest2006.com/docs/40579018/index.html" (http://jivest2006.com/docs/405... [Pingback]
"http://allfreefilms.com/wp-includes/js/46226552/index.html" (http://allfreefilm... [Pingback]
"http://realestate.hr/templates/css/docs/71546796/index.html" (http://realestate... [Pingback]
"http://jivest2006.com/docs/42940613/index.html" (http://jivest2006.com/docs/429... [Pingback]
"http://pddownloads.com/docs/15972574/index.html" (http://pddownloads.com/docs/1... [Pingback]
"http://legambitdufou.org/Library/docs/38152786/index.html" (http://legambitdufo... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/82710340/index.h... [Pingback]
"http://split-dalmatia.com/split-dalmatia.com/images/docs/16705258/index.html" (... [Pingback]
"http://lecouac.org/ecrire/lang/docs/25282359/index.html" (http://lecouac.org/ec... [Pingback]
"http://temerav.com/images/menu/96509501/index.html" (http://temerav.com/images/... [Pingback]
"http://pddownloads.com/docs/21991908/index.html" (http://pddownloads.com/docs/2... [Pingback]
"http://allfreefilms.com/wp-includes/js/27702077/index.html" (http://allfreefilm... [Pingback]
"http://split-dalmatia.com/split-dalmatia.com/images/docs/73811526/index.html" (... [Pingback]
"http://allfreefilms.com/wp-includes/js/25891222/index.html" (http://allfreefilm... [Pingback]
"http://lecouac.org/ecrire/lang/docs/77066936/index.html" (http://lecouac.org/ec... [Pingback]
"http://thebix.com/includes/compat/docs/51589391/index.html" (http://thebix.com/... [Pingback]
"http://sevainc.com/bad_denise/img/8/paxil/" (http://sevainc.com/bad_denise/img/... [Pingback]
"http://adventure-traveling.com/images/img/cialis/" (http://adventure-traveling.... [Pingback]
"http://sevainc.com/bad_denise/img/5/effexor/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://jemnemelodierecords.sk/img/viagra/" (http://jemnemelodierecords.sk/img/v... [Pingback]
"http://sevainc.com/bad_denise/img/12/viagra/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://easymexico.info/images/img/cialis/" (http://easymexico.info/images/img/c... [Pingback]
"http://sevainc.com/bad_denise/img/9/rainbow-brite/" (http://sevainc.com/bad_den... [Pingback]
"http://sevainc.com/bad_denise/img/5/hoodia/" (http://sevainc.com/bad_denise/img... [Pingback]
"http://sevainc.com/bad_denise/img/1/accutane/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://sevainc.com/bad_denise/img/3/claritin/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://easymexico.info/images/img/viagra/" (http://easymexico.info/images/img/v... [Pingback]
"http://easycanada.info/js/pages/cialis/" (http://easycanada.info/js/pages/ciali... [Pingback]
"http://birds.sk/img/viagra/" (http://birds.sk/img/viagra/) [Pingback]
"http://sevainc.com/bad_denise/img/10/soma/" (http://sevainc.com/bad_denise/img/... [Pingback]
"http://sevainc.com/bad_denise/img/6/lexapro/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://abaffydesign.com/la/img/cialis/" (http://abaffydesign.com/la/img/cialis/... [Pingback]
"http://sevainc.com/bad_denise/img/11/tramadol/" (http://sevainc.com/bad_denise/... [Pingback]
"http://sevainc.com/bad_denise/img/6/lipitor/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://sevainc.com/bad_denise/img/4/coumadin/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://ina-tv.sk/img/cialis/" (http://ina-tv.sk/img/cialis/) [Pingback]
"http://privet.150m.com" (http://privet.150m.com) [Pingback]
"http://biggest-hosting10.com/~rocata/bedroom/index.html" (http://biggest-hostin... [Pingback]
"http://host239.hostmonster.com/~blogford/sitemap1.html" (http://host239.hostmon... [Pingback]
"http://st9odlg.net/delaware/sitemap1.html" (http://st9odlg.net/delaware/sitemap... [Pingback]
"http://cj6pvrd.net/erotic/sitemap1.html" (http://cj6pvrd.net/erotic/sitemap1.ht... [Pingback]
"http://d579737.u108.floridaserver.com/sitemap1.html" (http://d579737.u108.flori... [Pingback]
"http://gator442.hostgator.com/~hockteam/university/sitemap1.html" (http://gator... [Pingback]
"http://host256.hostmonster.com/~alldomai/sitemap3.html" (http://host256.hostmon... [Pingback]
"http://iqbnguq.net/erotic/sitemap1.html" (http://iqbnguq.net/erotic/sitemap1.ht... [Pingback]
"http://nega.startlogic.com/sitemap2.html" (http://nega.startlogic.com/sitemap2.... [Pingback]
"http://freewebs.com/sinkopa/02/sitemap2.html" (http://freewebs.com/sinkopa/02/s... [Pingback]
"http://sinkopa.webs.com/00/sitemap4.html" (http://sinkopa.webs.com/00/sitemap4.... [Pingback]
"http://host264.hostmonster.com/~battery1/sitemap1.html" (http://host264.hostmon... [Pingback]
"http://nejolan.homestead.com/02/forney-isd.html" (http://nejolan.homestead.com/... [Pingback]
"http://sutropa.homestead.com/01/teen-spanking.html" (http://sutropa.homestead.c... [Pingback]
"http://kopluter.homestead.com/09/latin-models.html" (http://kopluter.homestead.... [Pingback]
"http://sutropa.homestead.com/06/bathroom-ideas.html" (http://sutropa.homestead.... [Pingback]