XML validation error: element not declared

I am creating a web service in .NET that will pass data back and forth through XML. I would like to validate XML in incoming requests using the XSD that I defined.

Here is the XSD:

<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="POSearch"> <xs:sequence minOccurs="0" maxOccurs="10"> <xs:element name="POID" type="xs:positiveInteger"/> </xs:sequence> </xs:complexType> </xs:schema> 

Here is the XML:

 <POSearch> <POID>1</POID> <POID>2</POID> </POSearch> 

Here is the verification code in C #:

 static void Main(string[] args){ XmlSchemaSet iSchemas = new XmlSchemaSet(); iSchemas.Add(string.Empty, @"...xsd file location"); XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema; settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation; settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); settings.Schemas.Add(iSchemas); XmlReader reader = XmlReader.Create(@"...xml file location", settings); try { while(reader.Read()) ; } catch(Exception ex) { Console.WriteLine(ex.Message); } } private static void ValidationCallBack(object sender, ValidationEventArgs args) { if(args.Severity == XmlSeverityType.Warning) Console.WriteLine("\tWarning: Matching schema not found. No validation occurred." + args.Message); else Console.WriteLine("\tValidation error: " + args.Message); } 

I feel like I was working on it before, and I'm not quite sure why this is not working now. Whenever I run this, I get the following exception message:

Validation error: POSearch element not declared.

Am I defining my XSD incorrectly? Is my verification code incorrect? Elements are all clear there. Any help pointing me in the right direction is greatly appreciated.

+6
source share
2 answers

You have a declared type, but no element is declared of this type.

Add item declaration:

 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="POSearch" type="POSearch"/> <xs:complexType name="POSearch"> <xs:sequence minOccurs="0" maxOccurs="10"> <xs:element name="POID" type="xs:positiveInteger"/> </xs:sequence> </xs:complexType> </xs:schema> 
+6
source

Try the following:

 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="POSearch"> <xs:sequence minOccurs="0" maxOccurs="10"> <xs:element name="POID" type="xs:positiveInteger"/> </xs:sequence> </xs:complexType> <xs:element name="POSearch" type="POSearch"/> </xs:schema> 
+4
source

All Articles