Link to home
Start Free TrialLog in
Avatar of Russ Suter
Russ Suter

asked on

(XML) The element is not declared

This is a super simple application (code attached). Its only purpose is to validate an XML document against and XSD schema. I've included a sample XML document and sample XSD schema for reference.

The program runs. It validates. Unfortunately, in the included example all I get back is "The 'Address' element is not declared." It IS declared! I declared it. WTF?

While on the subject I have a related question. How can I modify the code so it will evaluate the entire document for errors and not just give up after the first one it encounters?
<?xml version="1.0" encoding="utf-8"?>
<Address>
  <City>Beverly Hills</City>
  <State>CA</State>
  <Zip>90210</Zip>
</Address>

Open in new window

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Address">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="City" type="xs:string" />
        <xs:element name="State" type="xs:string" />
        <xs:element name="Zip" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Open in new window

XML-Validator.zip
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Well, your schema is correct and the XML is valid according to the schema
So I assume that the validator does not pick up the reference to the schema
You either should have some sort of catalogue,
or have a reference to the schema inside the xml, like this

<?xml version="1.0" encoding="utf-8"?>
<Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="file:/C:/.../address.xsd">
    <City>Beverly Hills</City>
    <State>CA</State>
    <Zip>90210</Zip>
</Address>
Avatar of Russ Suter
Russ Suter

ASKER

I discovered the problem. It was in how I was adding the schema. Apparently I was doing it incorrectly. The attached code fixes the issue.
XmlReader schemaReader = XmlReader.Create(schemaPath);
            xmlSettings.Schemas.Add(null, schemaReader);
            schemaReader.Close();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your explanation was unclear and unhelpful. I ended up finding the solution on my own.