XML schema checking readymade class.


You can check XML Schema of your xml file using bellow class, it is written in asp.net c#. XMLTextReader and XmlValidatingReader are used for schema checking. XML Schema means syntax or structure of your own created xml file. You only need to create the object of the class and pass xml and xsd file path.

1. ReadyMade class - Save bellow code in a file with name 'XMLValidation.cs'


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Schema;


public class XMLValidation
{
private string xmlFileName;
private string xmlSchemaFileName;
private XmlSchemaCollection objXmlSchemaCollection;
private bool isFailure;
public string Message;

public XMLValidation(string XMLFileName, string SchemaFileName)
{
xmlFileName = XMLFileName;
xmlSchemaFileName = SchemaFileName;
objXmlSchemaCollection = new XmlSchemaCollection();
objXmlSchemaCollection.Add(null, xmlSchemaFileName);
}

public bool Validate()
{
isFailure = false;
XmlTextReader objXmlTextReader = null;
XmlValidatingReader objXmlValidatingReader = null;

try
{
objXmlTextReader = new XmlTextReader(xmlFileName);
objXmlValidatingReader = new XmlValidatingReader(objXmlTextReader);
objXmlValidatingReader.Schemas.Add(objXmlSchemaCollection);

objXmlValidatingReader.ValidationEventHandler += new ValidationEventHandler(ValidationFailed);

while (objXmlValidatingReader.Read())
{
}

return isFailure;
}
catch (Exception ex)
{
Message = "Exception : " + ex.Message;
return true;
}
finally
{
objXmlValidatingReader.Close();
objXmlTextReader.Close();
}
}

private void ValidationFailed(object sender, ValidationEventArgs args)
{
isFailure = true;
Message = "Invalid XML File: " + args.Message;
}
}


2. As an example for how to use abow class for checking see bellow code.

XMLValidation objXMLValidation = new XMLValidation(Server.MapPath("") + "\\XMLStudent.xml", Server.MapPath("") + "\\XMLStudent.xsd");
if (objXMLValidation.Validate())
{
Label1.Text = objXMLValidation.Message;
return;
}


3. Example of xml file - save it as XMLStudent.xml

<?xml version="1.0" encoding="utf-8" ?>
<Students>
<Student Name="Roshan Krishna">
<Age>23</Age>
<Group>Science</Group>
</Student>
<Student Name="Jamaal Malik">
<Age>23</Age>
<Group>Science</Group>
</Student>
<Student Name="Nikitha Sahadev">
<Age>23</Age>
<Group>Science</Group>
</Student>
</Students>


4. Bellow is the schema of abow xml file - save it as XMLStudent.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Students">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Student">
<xs:complexType>
<xs:sequence>
<xs:element name="Age" type="xs:unsignedByte" />
<xs:element name="Group" type="xs:string" />
</xs:sequence>
<xs:attribute name="Name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>



Thanks.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: