when i run the code after adding both xml and xsd schema file i get the error as "ID" attribute not declared,The element 'Student' has invalid child element 'Student'. Validation failed below is my code:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <Students> <Student ID="101"> <Name>kamal</Name> <Gender>Male</Gender> <Marks>800</Marks> </Student> <Student ID="102"> <Name>Sapna</Name> <Gender>Female</Gender> <Marks>900</Marks> </Student> <Student ID="103"> <Name>Raju</Name> <Gender>Male</Gender> <Marks>870</Marks> <Student ID="104"> <Name>Sushant</Name> <Gender>Male</Gender> <Marks>700</Marks> </Student> </Student> </Students>
<?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="Marks" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
And finally the code file:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; using System.Xml.Schema; namespace ClassLibrary1 { class Class16 { public static void Main() { XmlSchemaSet ss = new XmlSchemaSet(); ss.Add("", @"C:\\Users\\admin\\documents\\visual studio 2013\\Projects\\ClassLibrary1\\ClassLibrary1\\Students.xsd"); XDocument doc = XDocument.Load(@"C:\\Users\\admin\\documents\\visual studio 2013\\Projects\\ClassLibrary1\\ClassLibrary1\\sample1.xml"); bool validationerror = false; doc.Validate(ss,(s,e)=> { Console.WriteLine(e.Message); validationerror = true; }); if(validationerror) { Console.WriteLine("Validation failed"); Console.ReadLine(); } else { Console.WriteLine("Validation succeded"); Console.ReadLine(); } } } }
you have to change in both XSD as well as in you sample.xml. For your reference Please find below correct XSD and xml files.
Correct Sample:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Students>
<Student ID="101">
<Name>kamal</Name>
<Gender>Male</Gender>
<Marks>800</Marks>
</Student>
<Student ID="102">
<Name>Sapna</Name>
<Gender>Female</Gender>
<Marks>900</Marks>
</Student>
<Student ID="103">
<Name>Raju</Name>
<Gender>Male</Gender>
<Marks>870</Marks>
</Student>
<Student ID="104">
<Name>Sushant</Name>
<Gender>Male</Gender>
<Marks>700</Marks>
</Student>
</Students>
Correct XSD:
<?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="Marks" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="ID" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.