The element is not declared

An element, usually the root, is not declared in the schema for its namespace.

The message, as .NET reports it
The 'invoice' element is not declared.

Why it happens

  • The wrong schema for this document, or a root element whose name is misspelt.
  • A schema version that doesn't have the element yet, or declares it only locally inside another element.
  • For an element in a namespace, .NET adds it: 'invoice' in namespace 'urn:…'.

How to fix it

Check that the element's name matches a global xs:element of the schema. Mind the quieter variant: when no schema in the set has the element's namespace at all (a missing or different xmlns), .NET only warns, Could not find schema information for the element, and without XmlSchemaValidationFlags.ReportValidationWarnings the document passes without being checked.

.NET throws an XmlSchemaValidationException at the first schema error unless XmlReaderSettings.ValidationEventHandler is set, which then receives every one with its line and position. A well-formedness error is an XmlException and ends reading whatever the settings.

The same problem in xmlvalid

xmlvalid validates with libxml2, which reports it as No matching global declaration available for the validation root. It checks the whole document in your browser and lists every schema error with its line and column, not only the first.

Element 'invoice': No matching global declaration available for the validation root.

Example

Validated against order.xsd, below.

Reports the error
<invoice currency="EUR">
  <id>A-1</id>
  <qty>1</qty>
</invoice>
Fixed
<order currency="EUR">
  <id>A-1</id>
  <qty>1</qty>
</order>
order.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="id" type="xs:string"/>
        <xs:element name="qty" type="xs:int"/>
        <xs:element name="status" type="Status" minOccurs="0"/>
        <xs:element name="code" type="Code" minOccurs="0"/>
        <xs:element name="note" type="Short" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute name="currency" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="Status">
    <xs:restriction base="xs:string">
      <xs:enumeration value="open"/>
      <xs:enumeration value="paid"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="Code">
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z]{3}-[0-9]{4}"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="Short">
    <xs:restriction base="xs:string">
      <xs:maxLength value="10"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>