Smartssolutions

Tuesday, May 24, 2011

Validate Xml content agains an XSD Schema using C#

This issue is often a warm subject in forums so I decided to post a clean and clear way to achive that with the minimum endeavour. Here is the code bellow. You have to just create a console application then copy and past the staff but dont forget to set the files contents and locations appropriately

using System;
using System.Xml;
using System.Xml.Schema;

namespace MSDN
{
  class Program
  {
    static bool validation = true;
    static void Main(string[] args)
    {
      XmlSchemaSet sc = new XmlSchemaSet();
      sc.Add(null, "file:///C:/temp/Schema2.xsd");

      // Set the validation settings.
      XmlReaderSettings settings = new XmlReaderSettings();
      settings.ValidationType = ValidationType.Schema;
      settings.Schemas = sc;
      settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

      // Create the XmlReader object.
      XmlReader reader = XmlReader.Create(@"C:\temp\Student.xml", settings);

      // Parse the file. 
      while (reader.Read()) ;

      if (validation==true)
      {
        Console.WriteLine("The Xml content is valid");
      }

      Console.ReadLine();
    }
    // Display any validation errors.
    private static void ValidationCallBack(object sender, ValidationEventArgs e)
    {
      validation = false;
      Console.WriteLine("Validation Error: {0}", e.Message);
    }  
  }
}

Here are the files

The Student.xml
<?xml version="1.0" encoding="utf-8"?>
<Sis>
  <Student>
    <StudentID>10042325</StudentID>
    <AcademicDetails>2008-2009</AcademicDetails>
    <PersonalDetails>
      <FirstName>Vijay</FirstName>
      <MiddleName>Laxmanrao</MiddleName>
      <LastName>Jadhav</LastName>
    </PersonalDetails>
  </Student>
</Sis>

The Schema1.xsd
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema id="Sis" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="PersonalDetails">
    <xsd:sequence>
      <xsd:element name="FirstName" type="xsd:string" minOccurs="1" />
      <xsd:element name="MiddleName" type="xsd:string" />
      <xsd:element name="LastName" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="StudentType">
    <xsd:sequence>
      <xsd:element name="StudentID" type="xsd:int" />
      <xsd:element name="AcademicDetails" type="xsd:string" />
      <xsd:element name="PersonalDetails" type="PersonalDetails" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="Sis">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Student" type="StudentType" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>
The Schema2.xsd
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema id="Sis" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="Personal">
    <xsd:sequence>
      <xsd:element name="First" type="xsd:string" minOccurs="1" />
      <xsd:element name="Middle" type="xsd:string" />
      <xsd:element name="Last" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="Student">
    <xsd:sequence>
      <xsd:element name="Identifier" type="xsd:int" />
      <xsd:element name="Academic" type="xsd:string" />
      <xsd:element name="Personal" type="Personal" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="Sis">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Student" type="Student" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Those schemas are used  to test the above code so you have to put them in the C:\temp directory








No comments:

Post a Comment