Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Validation error: The element is not declared

I am building a web service in .NET that will pass data back and forth via XML. I would like to validate the XML in the incoming requests using an XSD that I have 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 validation 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 had this working before and I'm not completely sure why this isn't working now. Whenever I run this I get the following Exception Message:

Validation error: The 'POSearch' element is not declared.

Have I defined my XSD wrong? Is my validation code wrong? The elements are all clearly there. Any help pointing me in the right direction is greatly appreciated.

like image 658
mac Avatar asked Jan 07 '16 14:01

mac


People also ask

What is an XML validation error?

XML validation is a process done to check for a syntax error in an XML document to ensure that the document is well-written with the standard rules using either DTD or schema. A complete XML file is considered to be valid XML document unless it has correct syntax and constraints defined in it.

How do I validate an XML file?

XML documents are validated by the Create method of the XmlReader class. To validate an XML document, construct an XmlReaderSettings object that contains an XML schema definition language (XSD) schema with which to validate the XML document.

Can Notepad ++ validate XML?

Notepad++ in combination with the XML Tools Plugin and the XidML schema is a smart way to validate XidML files (basically any XML file where you have a schema).

What is XML document validation?

XML validation is the process of determining whether the structure, content, and data types of an XML document are valid. XML validation also strips off ignorable whitespace in the XML document.


2 Answers

You have the type declared but no element declared of that type.

Add an element 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>
like image 93
kjhughes Avatar answered Sep 19 '22 09:09

kjhughes


Try this:

<?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>
like image 44
Kachna Avatar answered Sep 23 '22 09:09

Kachna