I’m working with C# and .NET 8.0.6.
I have an XSD that declares a “root” element with xs:token
content. When I validate a document with an instance element that has padded content, the padding is preserved? I’m expecting the padding (leading and trailing whitespace) to be removed.
The XSD is:
<code><?xml version='1.0'?>
<xs:schema
targetNamespace = "http://example.org/scratch"
xmlns = "http://example.org/scratch"
xmlns:xs = "http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="xs:token"/>
</xs:schema>
</code>
<code><?xml version='1.0'?>
<xs:schema
targetNamespace = "http://example.org/scratch"
xmlns = "http://example.org/scratch"
xmlns:xs = "http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="xs:token"/>
</xs:schema>
</code>
<?xml version='1.0'?>
<xs:schema
targetNamespace = "http://example.org/scratch"
xmlns = "http://example.org/scratch"
xmlns:xs = "http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="xs:token"/>
</xs:schema>
The instance is:
<code><?xml version='1.0'?>
<pre:root xmlns:pre="http://example.org/scratch"> abc </pre:root>
</code>
<code><?xml version='1.0'?>
<pre:root xmlns:pre="http://example.org/scratch"> abc </pre:root>
</code>
<?xml version='1.0'?>
<pre:root xmlns:pre="http://example.org/scratch"> abc </pre:root>
My code that does the following:
- Reads the XSD as a System.Xml.Schema.
XmlSchema
. - Adds the schema to a System.Xml.Schema.
SchemaSet
which is then compiled. - Creates an
XmlReader
(XsdValidatingReader
) using:
<code> var xmlReaderSettings = new XmlReaderSettings
{
CheckCharacters = true,
DtdProcessing = DtdProcessing.Prohibit,
Schemas = xmlSchemaSet,
ValidationType = ValidationType.Schema,
};
</code>
<code> var xmlReaderSettings = new XmlReaderSettings
{
CheckCharacters = true,
DtdProcessing = DtdProcessing.Prohibit,
Schemas = xmlSchemaSet,
ValidationType = ValidationType.Schema,
};
</code>
var xmlReaderSettings = new XmlReaderSettings
{
CheckCharacters = true,
DtdProcessing = DtdProcessing.Prohibit,
Schemas = xmlSchemaSet,
ValidationType = ValidationType.Schema,
};
- Loads the instance using System.Xml.
XmlDocument.Load(xmlReader)
.
The document object yields:
<code><?xml version="1.0"?>
<pre:root xmlns:pre="http://example.org/scratch"> abc </pre:root>
</code>
<code><?xml version="1.0"?>
<pre:root xmlns:pre="http://example.org/scratch"> abc </pre:root>
</code>
<?xml version="1.0"?>
<pre:root xmlns:pre="http://example.org/scratch"> abc </pre:root>
In particular, the XmlDocument.DocumentElement has:
- InnerXml: ” abc “
- NamespaceURI: “http://example.org/scratch”
- SchemaInfo.SchemaType.TypeCode: “Token”