Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 8 - XML validation against XSD

Suggested Videos
Part 5 - Transforming XML to CSV using LINQ to XML
Part 6 - Transforming XML to HTML table using LINQ to XML
Part 7 - Transform one XML format to another XML format



In this video, we will discuss validating an XML file against an XSD (XML Schema Definition Language) file.

What is an XSD file
An XSD ( XML Schema Definition Language) file defines the structure of the XML file, i.e which elements in which order, how many times, with which attributes, how they are nested, etc. Without an XSD, an XML file is a relatively free set of elements and attributes. 



Steps to validate an XML file using XSD file

Step 1 : Create a new Console application. Name it Demo.

Step 2 : Add a new XML Schema file to the project. Name it Student.xsd. Copy and paste the following XML.
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="Students">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Student" minOccurs="1" maxOccurs="4">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="Name" minOccurs="1" maxOccurs="1"/>
              <xsd:element name="Gender" minOccurs="1" maxOccurs="1"/>
              <xsd:element name="TotalMarks" minOccurs="1" maxOccurs="1"/>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

The above XSD specifies that
1. The root element must be Students
2. Students root element should contain at least 1 Student element. More than 4 Student elements are not allowed.
3. Each Student element should contain the following 3 elements in the order specified. 
    i) Name
    ii) Gender 
    iii) TotalMarks

Step 3: Add a new XML file to the project. Name it Data.xml. Copy and paste the following XML.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Students>
  <Student>
    <Name>Mark</Name>
    <Gender>Male</Gender>
    <TotalMarks>800</TotalMarks>
  </Student>
  <Student>
    <Name>Rosy</Name>
    <Gender>Female</Gender>
    <TotalMarks>900</TotalMarks>
  </Student>
  <Student>
    <Name>Pam</Name>
    <Gender>Female</Gender>
    <TotalMarks>850</TotalMarks>
  </Student>
  <Student>
    <Name>John</Name>
    <Gender>Male</Gender>
    <TotalMarks>950</TotalMarks>
  </Student>
</Students>

Step 4 : To validate Data.xml against Student.xsd file, copy and paste the following code in the Main() method.
XmlSchemaSet schema = new XmlSchemaSet();
schema.Add("", @"C:\Demo\Demo\Student.xsd");

XDocument xmlDocument = XDocument.Load(@"C:\Demo\Demo\Data.xml");
bool validationErrors = false;

xmlDocument.Validate(schema, (s, e) =>
{
    Console.WriteLine(e.Message);
    validationErrors = true;
});

if (validationErrors)
{
    Console.WriteLine("Validation failed");
}
else
{
    Console.WriteLine("Validation succeeded");
}

Note: Please include the following namespaces
using System;
using System.Linq;
using System.Xml.Linq;
using System.Xml.Schema;

Step 5 : Run the application. Since the XML in Data.xml confirms to the XSD file, we get the message Validation succeeded.

Step 6 : Remove <Name> element from one of the <Student> elements in Data.xml file. Run the application again. Notice that the validation fails and we get the following error.
The element 'Student' has invalid child element 'TotalMarks'. List of possible elements expected: 'Gender'.
Validation failed

LINQ to XML tutorial

No comments:

Post a Comment

It would be great if you can help share these free resources