Рет қаралды 63,288
In this video, we will discuss validating an XML file against an XSD (XML Schema Definition Language) file.
Text version of the video
csharp-video-tutorials.blogspo...
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our KZfaq channel. Hope you can help.
/ @aarvikitchen5572
Slides
csharp-video-tutorials.blogspo...
LINQ to SQL Tutorial - All Text Articles & Slides
csharp-video-tutorials.blogspo...
LINQ to XML Tutorial Playlist
• LINQ to XML Tutorial
Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses
kzfaq.info...
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="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 atleast 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 again 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