Validate Xml using Xsd using C#.Net

Posted by Techie Cocktail | 9:54 PM | , | 1 comments »

Say you have an application architecture that reads data from xml file, processes it and further renders or input data to another system. The xml element values should have correct data types, the attributes associated to the elements must have the correct types and should not be empty and so on. There can definitely be inconsistencies in the data.

You must ensure that the xml data that you receive is consistent so as not to break your application or further process invalid data. This article shows how you can validate your xml data source using XSD validation type before further processing it.

The first step is to prepare a schema definition file that sets or define rules for your xml source and based on these rules you can further build your application architecture. This conforms that your application will always receive valid xml data.

Lets see an example,

XML File Source: sites.xml


<?xml version="1.0"?>
<refSites>
<site name="techiecocktail">
<url>http://techiecocktail.blogspot.com</url>
<articles>100</articles>
</site>
<site name="xyz">
<url>http:///xyz.com</url>
<articles>50</articles>
</site>
</refSites>

The schema rules file defined for this xml is as below. You can have your own rules defined as per the requirements.

Schema File: siteSchema.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="generic" elementFormDefault="qualified" targetNamespace="generic">
<xsd:element name="refSites">
<xsd:complexType>
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="site">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="url" type="xsd:string">
</xsd:element>
<xsd:element name="articles" type="xsd:int">
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>




Now we have our xml source and its rules defined in the xsd file. Lets use this and build our xml validation logic in c# as below.

XML Valiation:

The required classes that you would require are XMLReaderSettings and XMLReader from System.Xml namespace.

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

namespace ValidateXML
{
class Program
{
static void Main(string[] args)
{
string appDir = System.AppDomain.CurrentDomain.BaseDirectory;

try
{
//Read the xml and xsd files
XmlTextReader _xmlTextReader = new XmlTextReader(appDir + @"..\..\sites.xml");
StreamReader _streamReader = new StreamReader(appDir + @"..\..\siteSchema.xsd");

//create schema object by reading the streamReader required by XMLReaderSettings object
XmlSchema _xmlSchema = new XmlSchema();
_xmlSchema = XmlSchema.Read(_streamReader, new ValidationEventHandler(_xmlReaderSettings_ValidationEventHandler));

//create XMLReaderSettings object and define the validation type
XmlReaderSettings _xmlReaderSettings = new XmlReaderSettings();
_xmlReaderSettings.ValidationType = ValidationType.Schema;
_xmlReaderSettings.Schemas.Add(_xmlSchema);

//define the callback eventhandler that will be called when there are validation errors
_xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler(_xmlReaderSettings_ValidationEventHandler);

//create the XMLReader object by passing the XMLTextReader and XMLReaderSettings objects
//and read the complete xml file and validating it using the defined schema.
XmlReader _xmlReader = XmlReader.Create(_xmlTextReader, _xmlReaderSettings);
while (_xmlReader.Read()) ;

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

//Callback EventHandler to display error message occured during validation.
static void _xmlReaderSettings_ValidationEventHandler(object sender, ValidationEventArgs e)
{
Console.Write(e.Message);
Console.Read();
}
}
}

Try to change any data in xml file that does not conform the schema rules. For example, in the xml file, if you change the 'articles' value to a string, the validation EventHandler will be called displaying the error details in the console window.

There are other available validation types such as DTD & XDR, schema validation being the most used one.

1 comments

  1. Kathy // May 6, 2009 at 5:18 PM  

    Thanks. Good work!! The examples are really good for understanding.