(XML) The element is not declared

Posted on 2011-10-06
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"?>
  <City>Beverly Hills</City>

Open in new window

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="">
  <xs:element name="Address">
        <xs:element name="City" type="xs:string" />
        <xs:element name="State" type="xs:string" />
        <xs:element name="Zip" type="xs:string" />

Open in new window
Question by:Russ Suter
    LVL 60

    Expert Comment

    by:Geert Bormans
    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=""
        <City>Beverly Hills</City>
    LVL 19

    Author Comment

    by:Russ Suter
    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);

    Open in new window

    LVL 60

    Accepted Solution

    as I said, wrong reference :-)
    LVL 19

    Author Closing Comment

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

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Problem with C# 1 44
    Specific format 21 107
    How to obtain string using substring and indexOf function 11 25
    Linq help 1 0
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now