Monday, April 20, 2009

Validation of XML with XSD in C#

This article explains about how to validate an XML document with XSD schema. The validation is performed by checking whether the XML document is a well-formed one by programmatically using .NET classes in C#.

Xml Document

xml version="1.0" encoding="utf-8" ?>

<Employees>

<Employee>

<FirstName>PankajFirstName>

<LastName>SahaLastName>

<Age>26Age>

<Address>

<City>BarodaCity>

<Country>IndiaCountry>

Address>

Employee>

<Employee>

<FirstName>AnimeshFirstName>

<LastName>SingLastName>

<Age>27Age>

<Address>

<City>BarodaCity>

<Country>IndiaCountry>

Address>

Employee>

Employees >


Here I have created two xsd file, one is valid or another is invalid.

You can create the xsd file from a xml file with the visual studio.

  1. Open the xml file in the visual studio.
  2. Go to the xml menu
  3. click on Create Schema



Figure-1



Figure-2



To created invalid xsd I have removed one tag LastName after the FirstName tag in the valid xsd.

Valid XSD schema

xml version="1.0" encoding="utf-8"?>

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Employees" >

<xs:complexType>

<xs:sequence maxOccurs ="unbounded" >

<xs:element maxOccurs="unbounded" name="Employee" >

<xs:complexType>

<xs:sequence>

<xs:element name="FirstName" type="xs:string" />

<xs:element name="LastName" type="xs:string" />

<xs:element name="Age" type="xs:unsignedByte" />

<xs:element name="Address">

<xs:complexType>

<xs:sequence>

<xs:element name="City" type="xs:string" />

<xs:element name="Country" type="xs:string" />

xs:sequence>

xs:complexType>

xs:element>

xs:sequence>

xs:complexType>

xs:element>

xs:sequence>

xs:complexType>

xs:element>

xs:schema>

Invalid XSD schema

xml version="1.0" encoding="utf-8"?>

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Employees">

<xs:complexType>

<xs:sequence>

<xs:element maxOccurs="unbounded" name="Employee">

<xs:complexType>

<xs:sequence>

<xs:element name="FirstName" type="xs:string" />

<xs:element name="Age" type="xs:unsignedByte" />

<xs:element name="Address">

<xs:complexType>

<xs:sequence>

<xs:element name="City" type="xs:string" />

<xs:element name="Country" type="xs:string" />

xs:sequence>

xs:complexType>

xs:element>

xs:sequence>

xs:complexType>

xs:element>

xs:sequence>

xs:complexType>

xs:element>

xs:schema>

To check whether the xml is valid or not I have done the following steps:

1. Created class XmlValidator

2. The ValidationCallBack event is used to define an event handler for receiving the notification about XSD schema validation errors. If any validation error occurs, an exception is thrown, and the XmlValidatingReader cannot be restarted

Conclusion

This article explained about the XML document, XSD schema, and how to validate XML document against XSD schema using Microsoft .NET framework classes.


Download:




No comments:

Post a Comment