Skip to content

Misleading exception when processing of included XSD fails #36923

@michalfi

Description

@michalfi

Description

When there is an exception during preprocessing an XML schema that uses XS:include, the exception is silently swallowed by the XmlSchemaSet code. Instead, a misleading exception is thrown only after Compile() is called on the schema set.

Minimal repro:

main.xsd:

<?xml version="1.0" encoding="UTF-8" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:example="https://www.example.org/schema" targetNamespace="https://www.example.org/schema">
	<include schemaLocation="./included.xsd"/>
	<element name="e" type="example:StringType"/>
</schema>

included.xsd:

<?xml version="1.0" encoding="windows-1252" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:example="https://www.example.org/schema" targetNamespace="https://www.example.org/schema">
	<simpleType name="StringType">
		<restriction base="string"/>
	</simpleType>
</schema>

code:

var set = new XmlSchemaSet() { XmlResolver = new XmlUrlResolver() };
var schema = XmlSchema.Read(File.OpenRead("./main.xsd"), (sender, e) => Console.WriteLine(e.Message));
set.Add(schema);
set.Compile();

In this case, the included schema is not added to the set, because it uses unsupported encoding and its preprocessing fails.
But the exception is not passed on, program only fails at the time Compile() is called, reporting an

XmlSchemaException: 'Type 'https://www.example.org/schema:StringType' is not declared.'

(because the type is defined in the included schema).

Configuration

This only happens in .NET core (any version as far as I know). Under .NET fw, the code runs fine.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions