Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

(XML) The element is not declared

Posted on 2011-10-06
4
Medium Priority
?
1,037 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:Russ Suter
  • 2
  • 2
4 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36927566
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>
0
 
LVL 20

Author Comment

by:Russ Suter
ID: 36927775
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

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1000 total points
ID: 36929983
as I said, wrong reference :-)
0
 
LVL 20

Author Closing Comment

by:Russ Suter
ID: 37429596
Your explanation was unclear and unhelpful. I ended up finding the solution on my own.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question