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?
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>
<?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>
XML-Validator.zip
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();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Your explanation was unclear and unhelpful. I ended up finding the solution on my own.
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:noNamespaceSchemaLocat
<City>Beverly Hills</City>
<State>CA</State>
<Zip>90210</Zip>
</Address>