Question

I'm baffled by a message XmlSchemaException is throwing at me. Let me provide some context. In my application I use two types of XML Reference tables: "IFU" and "CC", both have their own schemas defined. When I load the files I validate them by using these schemas and it's working perfectly. Until something goes wrong.

When the application is expecting the IFU ref table (IFU Schema selected) and I load the CC table instead, the error is detected, line number fits (2), but the message says:

e.Message = "The "CCReferenceTable" element is not declared."

while it should say

e.Message = "The "IFUReferenceTable" element is not declared."

Now, code snippets:

LoadXML routine

    public XmlDocument LoadXML(string filePath, string schemaFilePath)
    {
        Directory.SetCurrentDirectory(System.IO.Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory));
        if (!DoesFileExist(filePath))
            throw new Exception(String.Format("File {0} does not exist.", filePath));
        if (!DoesFileExist(schemaFilePath))
            throw new Exception(String.Format("File {0} does not exist.", schemaFilePath));

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.Schemas.Add(null, schemaFilePath);
        settings.ValidationType = ValidationType.Schema;

        XmlDocument document = new XmlDocument();
        using (XmlReader reader = XmlReader.Create(filePath, settings))
        {
            document.Load(reader);
        }
        return document;
    }

XmlSchema

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="IfuReferenceTable">
    //////////Other content
  </xs:element>
</xs:schema>

XML Refernce table

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IfuReferenceTable>
  //////////Other content
</IfuReferenceTable>

The CC Ref tables look the same save for the root node ("CCRefenerceTable" instead of "IFUReferenceTable").

What am I missing?

Was it helpful?

Solution

What you describe is a normal diagnostic.

When the application is expecting the IFU ref table (IFU Schema selected) and I load the CC table instead, the error is detected, line number fits (2), but the message says:

e.Message = "The "CCReferenceTable" element is not declared."

When a validating parser using the IfuReferenceTable schema ("IFU Schema selected", as you say) encounters a CCReferenceTable element for which it has no definition, it will correctly complain as it did.

Think of it this way: When the parser sees CCReferenceTable, it tries to lookup what it knows about CCReferenceTable. When it cannot find anything about CCReferenceTable, it lets you know with the above message.

Xerces-J would say it this way:

Cannot find the declaration of element 'CCReferenceTable'.

Perhaps this statement is a bit more clear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top