Are you looking for right? Are you also searching for a topic Part 8 XML validation against XSD? If this is the case then please see it right here
Mục lục bài viết
[button size=”medium” style=”primary” text=”WATCH VIDEO BELOW” link =”” target=””]
In this video, we will discuss validating an XML file against an XSD (XML Schema Definition Language) file.
Text version of the video
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 YouTube channel. Hope you can help.
Slides
LINQ to SQL Tutorial – All Text Articles & Slides
LINQ to XML Tutorial Playlist
Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses
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=”
[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:DemoDemoStudent.xsd”);
XDocument xmlDocument = XDocument.Load(@”C:DemoDemoData.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 ..
You can also view more information regarding internet tips about the game by us here: Bem2.vn
You can read more information Tutorials here:See more here.
validate xml against xsd c#,validate xml using xsd,dot net,.net,asp.net,c#,linq to xml tutorial,XML Schema,XML Validation,XML (Programming Language).
#Part #XML #validation #XSD.
We hope this information is useful to you, thank you very much for following this article.
In this video, we will discuss validating an XML file against an XSD (XML Schema Definition Language) file.
Text version of the video
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 YouTube channel. Hope you can help.
Slides
LINQ to SQL Tutorial – All Text Articles & Slides
LINQ to XML Tutorial Playlist
Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses
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=”
[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:DemoDemoStudent.xsd”);
XDocument xmlDocument = XDocument.Load(@”C:DemoDemoData.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 ..