C# - XML Schema Validator






4.90/5 (29 votes)
This article demonstrates the use of an XML Schema validation class.
Introduction
Have you ever worked on a project utilizing Web Services, XML, and Schemas? Then you probably have needed to ensure the validity of the XML being passed to the Web Service against the Schema. This article shows how to use .NET's XmlValidatingReader
to validate XML with a schema.
Using the code
- Create the
XmlSchemaValidator
class. - Create the XML Schema.
- Create a WebService and add the
ValidateXML
method.
Note: All of the code is available via the downloadable zip file.
XmlSchemaValidator Class
The XmlSchemaValidator
class contains an overloaded publicly available method ValidXmlDoc
. This method allows the caller to pass in either a string
of XML, an XmlDocument
, or a StringReader
of xml. The other parameters are the namespace and URI of the schema used to validate the XML. The method returns a boolean value, true
if the XML is valid, false
if the XML is invalid. There is also a publicly available string
"ValidationError
" that contains the validation error.
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
namespace YourNameSpace.GoesHere
{
/// <SUMMARY>
/// This class validates an xml string or xml document against an xml
/// schema.
/// It has public methods that return a boolean value depending on
/// the validation
/// of the xml.
/// </SUMMARY>
public class XmlSchemaValidator
{
private bool isValidXml = true;
private string validationError = "";
/// <SUMMARY>
/// Empty Constructor.
/// </SUMMARY>
public XmlSchemaValidator()
{
}
/// <SUMMARY>
/// Public get/set access to the validation error.
/// </SUMMARY>
public String ValidationError
{
get
{
return "<VALIDATIONERROR>" + this.validationError
+ "</VALIDATIONERROR>";
}
set
{
this.validationError = value;
}
}
/// <SUMMARY>
/// Public get access to the isValidXml attribute.
/// </SUMMARY>
public bool IsValidXml
{
get
{
return this.isValidXml;
}
}
/// <SUMMARY>
/// This method is invoked when the XML does not match
/// the XML Schema.
/// </SUMMARY>
/// <PARAM name="sender"></PARAM>
/// <PARAM name="args"></PARAM>
private void ValidationCallBack(object sender,
ValidationEventArgs args)
{
// The xml does not match the schema.
isValidXml = false;
this.ValidationError = args.Message;
}
/// <SUMMARY>
/// This method validates an xml string against an xml schema.
/// </SUMMARY>
/// <PARAM name="xml">XML string</PARAM>
/// <PARAM name="schemaNamespace">XML Schema Namespace</PARAM>
/// <PARAM name="schemaUri">XML Schema Uri</PARAM>
/// <RETURNS>bool</RETURNS>
public bool ValidXmlDoc(string xml,
string schemaNamespace, string schemaUri)
{
try
{
// Is the xml string valid?
if(xml == null || xml.Length < 1)
{
return false;
}
StringReader srXml = new StringReader(xml);
return ValidXmlDoc(srXml, schemaNamespace, schemaUri);
}
catch(Exception ex)
{
this.ValidationError = ex.Message;
return false;
}
}
/// <SUMMARY>
/// This method validates an xml document against an xml
/// schema.
public bool ValidXmlDoc(XmlDocument xml,
string schemaNamespace, string schemaUri)
{
try
{
// Is the xml object valid?
if(xml == null)
{
return false;
}
// Create a new string writer.
StringWriter sw = new StringWriter();
// Set the string writer as the text writer
// to write to.
XmlTextWriter xw = new XmlTextWriter(sw);
// Write to the text writer.
xml.WriteTo(xw);
// Get
string strXml = sw.ToString();
StringReader srXml = new StringReader(strXml);
return ValidXmlDoc(srXml, schemaNamespace, schemaUri);
}
catch(Exception ex)
{
this.ValidationError = ex.Message;
return false;
}
}
/// <SUMMARY>
/// This method validates an xml string against an xml schema.
/// </SUMMARY>
/// <PARAM name="xml">StringReader containing xml</PARAM>
/// <PARAM name="schemaNamespace">XML Schema Namespace</PARAM>
/// <PARAM name="schemaUri">XML Schema Uri</PARAM>
/// <RETURNS>bool</RETURNS>
public bool ValidXmlDoc(StringReader xml,
string schemaNamespace, string schemaUri)
{
// Continue?
if(xml == null || schemaNamespace == null || schemaUri == null)
{
return false;
}
isValidXml = true;
XmlValidatingReader vr;
XmlTextReader tr;
XmlSchemaCollection schemaCol = new XmlSchemaCollection();
schemaCol.Add(schemaNamespace, schemaUri);
try
{
// Read the xml.
tr = new XmlTextReader(xml);
// Create the validator.
vr = new XmlValidatingReader(tr);
// Set the validation tyep.
vr.ValidationType = ValidationType.Auto;
// Add the schema.
if(schemaCol != null)
{
vr.Schemas.Add(schemaCol);
}
// Set the validation event handler.
vr.ValidationEventHandler +=
new ValidationEventHandler(ValidationCallBack);
// Read the xml schema.
while(vr.Read())
{
}
vr.Close();
return isValidXml;
}
catch(Exception ex)
{
this.ValidationError = ex.Message;
return false;
}
finally
{
// Clean up...
vr = null;
tr = null;
}
}
}
}
XmlSchema Example
The following code is an example Schema file used to validate the example XML:
<xs:schema id="XSDSchemaTest"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
>
<xs:simpleType name="FamilyMemberType">
<xs:restriction base="xs:string">
<xs:enumeration value="384" />
<xs:enumeration value="385" />
<xs:enumeration value="386" />
<xs:enumeration value="" />
</xs:restriction>
</xs:simpleType>
<xs:element name="Answer">
<xs:complexType>
<xs:sequence>
<xs:element name="ShortDesc" type="FamilyMemberType" />
<xs:element name="AnswerValue" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML Example
Example XML validated against the schema:
<Answer>
<ShortDesc>385</ShortDesc>
<AnswerValue>1</AnswerValue>
</Answer>
WebService Example
The WebService has a publicly available method that validates the XML. This method provides an example to one of the many ways the validator object may be used. Remember, there are three overloads to the ValidXmlDoc
method.
[WebMethod]
public string ValidateXml(string xml)
{
XmlSchemaValidator validator = new XmlSchemaValidator();
string retVal = "<VALIDXML>" +
validator.ValidXmlDoc(xml, "",
Server.MapPath("XSDSchemaTest.xsd")).ToString() +
"</VALIDXML>";
if(!validator.IsValidXml)
{
retVal = retVal + validator.ValidationError;
}
return retVal;
}