Sunday, July 01, 2007

XML Validation



using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Schema;
namespace XMLVAlidation
{
class Program
{
private static bool isValid = true; // if validtation error happens set this flag false
static void Main(string[] args)
{
//XmlTextReader r = new XmlTextReader("C:\\XML\\ProductWithDTD.xml"); //to load product with DTD -validation type
//XmlTextReader r = new XmlTextReader("C:\\XML\\ProductWithXDR.xml"); ////to load product with XDR - vali type
XmlTextReader r = new XmlTextReader("C:\\XML\\Header.ascx.en-US.resx");
//XmlTextReader r = new XmlTextReader("C:\\XML\\ProductWithXSD.xml"); // validation type schema
XmlValidatingReader v = new XmlValidatingReader(r);
//v.ValidationType = ValidationType.DTD; //validation type for DTD
v.ValidationType = ValidationType.Schema; ////validation type for XDR
v.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);
while (v.Read())
{
// Can add code here to process the content.
}
v.Close();
// Check whether the document is valid or invalid.
if (isValid)
Console.WriteLine("Document is valid");
else
Console.WriteLine("Document is invalid");

Console.ReadLine();
}
public static void MyValidationEventHandler(object sender,ValidationEventArgs args)
{
isValid = false;
Console.WriteLine("Validation event\n" + args.Message);
}
}
}


Needed resources

Product.xml

< Product ProductID = "123" >
< ProductName > Rugby jersey < / ProductName >
< / Product >


ProductWithDTD.xml

< ? xml version = "1.0" ? >
< ! DOCTYPE Product SYSTEM "Product.dtd" >
< Product ProductID = "123" >
< ProductName > Rugby jersey< / ProductName >
< ProductID > 123 < / ProductID >
< / Product >


Product.dtd


< ! ELEMENT Product ( ProductName ) >
< ! ATTLIST Product ProductID CDATA #REQUIRED >
< ! ELEMENT ProductName ( # PCDATA ) >


ProductWithXSD.xml

< ? xml version = "1.0" ? >
< Product ProductID = "123"
xmlns : xsi="http: // www.w3.org/2001/XMLSchema-instance"
xsi : noNamespaceSchemaLocation="Product.xsd">
< ProductName > Rugby jersey < / ProductName >
< / Product >



Product.xsd

< ? xml version= "1.0" ? >
< xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
< xsd:element name="Product" >
< xsd:complexType >
< xsd:sequence >
< xsd:element name="ProductName" type="xsd:string" / >
< / xsd:sequence>
< xsd:attribute name="ProductID" use="required" type="xsd:int" / >
< / xsd:complexType >
< / xsd:element >
< / xsd:schema >


ProductWithXDR.xml

< ? xml version="1.0" ? >
< Product ProductID="123" xmlns="x-schema:Product.xdr" >
< ProductName > Rugby jersey < / ProductName >

< / Product >


Product.xdr

< ? xml version="1.0" ? >
< Schema name="ProductSchema"
xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes" >

< ElementType name="Product" content="eltOnly" >
< attribute type="ProductID" required="yes" / >
< element type="ProductName" / >
< / ElementType >

< AttributeType name="ProductID" dt:type="int" / >
< ElementType name="ProductName" dt:type="string" / >
< / Schema >

1 comment:

See The world in a different way said...

nice blog
keep on going