Part 8 XML validation against XSD

  Рет қаралды 63,288

kudvenkat

kudvenkat

Күн бұрын

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

Пікірлер: 34
@MrBookyWooky
@MrBookyWooky 6 жыл бұрын
Thank you. I like the fact that you use the same, or similar, simple example in all 8 videos to show the different LINQ xml functions. Clear and easy to follow.
@haroonnasir9103
@haroonnasir9103 2 жыл бұрын
Man what would i do without Venkat .... really saved my day ...
@shashank.maheshwari
@shashank.maheshwari 2 жыл бұрын
Thanks a lot. You saved my day, was struggling with xml validation issue for a while.
@anilgarlapati8998
@anilgarlapati8998 8 жыл бұрын
Thank you Venkat.
@khadarbasha6371
@khadarbasha6371 3 жыл бұрын
Thanks Venkat for the code..you explained it clearly and neatly..
@chandrunagrani2560
@chandrunagrani2560 10 жыл бұрын
Excellent work video can the XSD have rules that specify an element is required? Thanks
@ashu3769
@ashu3769 10 жыл бұрын
Thank you sir for uploading this.
@abdelhadi55
@abdelhadi55 5 жыл бұрын
Hi thx for the tutorial but I have a little question. When I do on my xsd file to test on my xml file to validate, IT WORKS. but it doesn't work with . Do you have any Idea please ??
@Nicetrycutiepie
@Nicetrycutiepie 3 жыл бұрын
Good Venkat, thank you so much for the example.
@kamalprathapsingh6817
@kamalprathapsingh6817 5 жыл бұрын
see i was trying this program acctually ,the problem is the validationserrors bool variable it can not set as true inside lambda .its showed as false .but you showed output correctly.
@edsalazar4590
@edsalazar4590 7 жыл бұрын
Thank you as always!
@dyachenkoserj
@dyachenkoserj 7 жыл бұрын
great! thank you!
@oneworldnoborders
@oneworldnoborders 4 жыл бұрын
You are so good. Thanks teacher.
@michawerner6773
@michawerner6773 2 жыл бұрын
Hi, thanks a lot. Your video still rocks
@mithunMADDY9
@mithunMADDY9 9 жыл бұрын
Excellent
@rodrigoantweber
@rodrigoantweber 9 жыл бұрын
Thank you. Upload videos on WPF
@shwetakashiv8807
@shwetakashiv8807 6 жыл бұрын
Can we validate some portion of XML file with xsd?
@Vishal1419251
@Vishal1419251 10 жыл бұрын
Sir, can you record a video on XML Encryption in LINQ to XML?
@sasankkotla9874
@sasankkotla9874 Жыл бұрын
I need to log(or print) the info when the validation is successful too. That is upon for every successful validation of xml element, how can i print(or console.write) that the specific xml element is validated successfully?
@christianarcano9212
@christianarcano9212 8 жыл бұрын
You're voice sounds familiar. Have you worked as voice-over / narrator on IT training materials?
@ravendfj
@ravendfj 5 жыл бұрын
Thank you.
@eX1m0
@eX1m0 4 жыл бұрын
thanks, nice tutorial
@dinavahire2908
@dinavahire2908 4 жыл бұрын
my xsd is on a path(uri) which has res:// as a prefix. for example res://path/file.xsd. How to add this schema in XmlSchemaSet. I tried every option but it says , URI prefix in not recognized. If anyone can help. Link of the stackover question: stackoverflow.com/questions/59071603/xmlreadersettings-schemas-add-the-uri-prefix-is-not-recognized
@nys8260
@nys8260 3 жыл бұрын
Thanks Venkat
@THIERNO5555
@THIERNO5555 8 жыл бұрын
Thanksss
@venkateshanasuri1790
@venkateshanasuri1790 3 жыл бұрын
How to cache the xmlschemaset using redis instead of fetching it every time for validation?
@nateTHEgrateful
@nateTHEgrateful 7 жыл бұрын
Very helpful series!
@Csharp-video-tutorialsBlogspot
@Csharp-video-tutorialsBlogspot 7 жыл бұрын
Thank you very much for taking time to give feedback. This means a lot. I am very glad you found the videos useful. I have organised all the Dot Net & SQL Server videos in to playlists, which could be useful to you kzfaq.infoplaylists?view=1&sort=dd If you need DVDs or to download all the videos for offline viewing please visit www.pragimtech.com/kudvenkat_dvd.aspx Slides and Text Version of the videos can be found on my blog csharp-video-tutorials.blogspot.com Tips to effectively use my youtube channel. kzfaq.info/get/bejne/r51oY7Cozb-bYKc.html If you want to receive email alerts, when new videos are uploaded, please subscribe to my youtube channel. kzfaq.info If you like these videos, please click on the THUMBS UP button below the video. May I ask you for a favor. I want these tutorials to be helpful for as many people as possible. Please share the link with your friends and family who you think would also benefit from them. Good Luck Venkat
@siddharth1996
@siddharth1996 4 жыл бұрын
@@Csharp-video-tutorialsBlogspot How can we define or handle following XML against XSD.? 《Students》 《Student Name="sunil" rollno=12》 《Student Name="Ram" rollno=13》 《/Students》
@MrJiturcm
@MrJiturcm 10 жыл бұрын
nice
@lsztmjr5240
@lsztmjr5240 6 жыл бұрын
How to false to true?
@siddharth1996
@siddharth1996 4 жыл бұрын
How can we define or handle following XML against XSD.? 《Students》 《Student Name="sunil" rollno=12》 《Student Name="Ram" rollno=13》 《/Students》
@maximemelyov
@maximemelyov 7 жыл бұрын
в
Part 5   Transforming XML to CSV using LINQ to XML
7:12
kudvenkat
Рет қаралды 21 М.
XML Schema (XSD) Beginner Tutorial with Demo
9:44
Automation Step by Step
Рет қаралды 344 М.
Женская драка в Кызылорде
00:53
AIRAN
Рет қаралды 495 М.
New model rc bird unboxing and testing
00:10
Ruhul Shorts
Рет қаралды 31 МЛН
IQ Level: 10000
00:10
Younes Zarou
Рет қаралды 6 МЛН
Part 4   Modifying xml document using linq to xml
12:50
kudvenkat
Рет қаралды 39 М.
13. Validating XML against XSD in C# using XmlReader
14:37
Brandan Jones
Рет қаралды 16 М.
XML Tutorial   34 Creating An XSD Schema
4:15
mrfizzlebutt
Рет қаралды 22 М.
Mapping XML documents with XSD | XML Tutorial | Mr. Satish B
22:11
Naresh i Technologies
Рет қаралды 33 М.
Part 1   LINQ to XML
8:46
kudvenkat
Рет қаралды 83 М.
XPath and C#: Get data from an XML Document
12:40
Brandan Jones
Рет қаралды 20 М.
Part 6   Transforming XML to HTML table using LINQ to XML
8:06
kudvenkat
Рет қаралды 17 М.
2. Create and Link an XSD file to an XML file in Notepad++
10:50
Brandan Jones
Рет қаралды 62 М.
Женская драка в Кызылорде
00:53
AIRAN
Рет қаралды 495 М.