.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Lee Hnetinka

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Validating XML Document Using C#

Posted By:Ravi Ranjan Kumar       Posted Date: June 22, 2011    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

This article enable you to validate XML document by using XMLReader in C#


A schema is used to define he structure of an XML document. An XML document is considered to be well formed if it confirms to all the syntactical requirement as defined by the W3C extensible Marku Language (XML) Recommendation. However, an XML document is not considered to be valid unless it confirms to the constrains defined by its schema in addition to being well formed. Hence, although the entire valid documents are well formed, not all well-formed XML document are valid.

We can validate an XML document against a schema by using the following two classes 

Validating an XML document using XMLReader

The XMLReader class can impose validation by using a schema Difinition language (XSD) schema. The validationType property on the XMLReaderSettings class determines whether the XmlReader instance imposes validation or not.

We can use an object of the XmlSchemaSet class to hold to cache of XSD schemas. This class is defined under the System.Xml.Schema namespace. The XmlSchemaSet improves performance by catching schemas in memory as otherwise they would have had to be accessed from a file or a Uniform Resource Loator(URL). Each schema in SmlScheaSet is identified by its namespace Uniform Resource Identifier (URI) and a schema location. The Schemas property of the XmlReaderSettings class assign the SmlSchemaSet object to use.

Practical approach

Now Consider a schema Named Products.xsd



Now Consider an XML file Products.xml using Products.xsd

		Barbie Doll

Now use the given code to validate the Products.xml file using Products.xsd 

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.IO;

namespace ValidateXMLSchema
    class Program
        static void Main(string[] args)
            XmlSchemaSet schemaset = new XmlSchemaSet();
            schemaset.Add("urn:product-schema", @"C:\XMLSchema\Products.xsd");
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas = schemaset;
            settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
            XmlReader reader = XmlReader.Create(@"C:\XMLSchema\Products.xml", settings);
            while (reader.Read()) ;         
        private static void ValidationCallBack(object sender, ValidationEventArgs e)

            Console.WriteLine("Errors: {0}", e.Message);   // Print when error found

The Given code will display Not Error Found because XML file using and following according to Schema.

if we will do minor change in sequence in xml file like given below 
Barbie Doll
100 // Changing Sequence
Then it will display error or if we will change amount of price to Negative like 
then it will display 

In the Preceding code, the namespace, urn:product-schema, identifies which schema in the XMlSchemaSet to use for validation.

So lets enjoy with validating XML document with XMLReader. 

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend