Public .NET API ReferenceL3 Persistence & IntegrationsStableAOT: Full

EricksonLopez.Xml

High-performance XSD validation engine with Anti-XXE defaults, thread-safe precompiled schema cache, Result-based errors, and Native AOT support for modern .NET.

1 namespaces7 types34 membersGenerated from XML docs & assemblies · UTC 2026-12-01

Showing 7 of 7 types (34 members)

EricksonLopez.Xml.Validation#

7
interface

IXmlSchemaCache

Assembly: EricksonLopez.Xml.Validation
public interface IXmlSchemaCache

Defines a thread-safe registry and cache for precompiled XSD schema sets.

Remarks:

Implementations must be thread-safe for concurrent registration and lookup operations. Precompiled `XmlSchemaSet` instances are cached to eliminate redundant compilation overhead and ensure safe schema resolution with external entity resolution disabled.

Members (7)

  • methodClear
    void Clear()

    Removes all registered schemas from the cache.

    This operation is thread-safe. Concurrent registrations or lookups in progress will complete normally; subsequent operations will see an empty cache.

  • methodContainsSchema
    bool ContainsSchema(string targetNamespace)

    Determines whether a schema for the specified target namespace is registered in the cache.

    targetNamespace: string
    The target XML namespace to look up

    Returns: `true` if a schema for the specified namespace is registered; otherwise, `false`.

    Exceptions: ArgumentNullException: `targetNamespace` is `null`

  • methodIsRootElementDeclared
    bool IsRootElementDeclared(string targetNamespace, string localName, string namespaceUri)

    Determines whether the specified root element qualified name is declared as a global element in the registered schema.

    targetNamespace: string
    The target XML namespace of the schema
    localName: string
    The local name of the root element
    namespaceUri: string
    The namespace URI of the root element

    Returns: `true` if the element is declared as a global element in the target schema; otherwise, `false`.

    Exceptions: ArgumentNullException: `targetNamespace`, `localName`, or `namespaceUri` is `null`

  • methodRegisterSchema
    void RegisterSchema(string targetNamespace, ReadOnlySpan<byte> utf8Xsd)

    Registers a precompiled XSD schema definition from the specified UTF-8 byte span.

    targetNamespace: string
    The target XML namespace associated with the schema
    utf8Xsd: ReadOnlySpan<byte>
    The UTF-8 encoded bytes representing the XSD schema

    Exceptions: ArgumentNullException: `targetNamespace` is `null`; XmlException: The bytes do not represent well-formed XML, or the span is empty; XmlSchemaException: The bytes contain schema structural or syntax errors

  • methodRegisterSchema
    void RegisterSchema(string targetNamespace, Stream xsdStream)

    Registers a precompiled XSD schema definition from the specified stream.

    targetNamespace: string
    The target XML namespace associated with the schema
    xsdStream: Stream
    The readable stream containing XSD schema data

    Exceptions: ArgumentNullException: `targetNamespace` or `xsdStream` is `null`; XmlException: The stream does not contain well-formed XML; XmlSchemaException: The stream contains schema structural or syntax errors

  • methodRegisterSchema
    void RegisterSchema(string targetNamespace, string xsdContent)

    Registers a precompiled XSD schema definition from the specified string.

    targetNamespace: string
    The target XML namespace associated with the schema
    xsdContent: string
    The raw XML Schema Definition (XSD) string to compile

    Exceptions: ArgumentNullException: `targetNamespace` or `xsdContent` is `null`; XmlException: The `xsdContent` is not well-formed XML; XmlSchemaException: The `xsdContent` contains schema structural or syntax errors

  • propertyCount
    int Count { get; }

    Gets the total number of precompiled schema sets currently registered in the cache.

interface

IXmlSchemaValidator

Assembly: EricksonLopez.Xml.Validation
public interface IXmlSchemaValidator

Defines a contract for validating XML documents against precompiled XSD schemas with Anti-XXE enforcement.

Remarks:

Implementations must enforce strict Anti-XXE security by prohibiting DTD processing and external entity resolution. Validation results are returned as strongly-typed `Result<T>` instances rather than throwing exceptions for validation failures. When used with the standard `XmlSchemaValidator` implementation, the supplied `IXmlSchemaCache` must be backed by a provider that exposes precompiled schema sets (such as `XmlSchemaCache`).

Members (4)

  • methodValidate
    Result<bool> Validate(ReadOnlySpan<byte> utf8Xml, string targetNamespace)

    Validates a UTF-8 encoded XML byte sequence against the precompiled schema associated with the specified namespace.

    utf8Xml: ReadOnlySpan<byte>
    The UTF-8 encoded byte sequence representing the XML document
    targetNamespace: string
    The target XML namespace of the schema to validate against

    Returns: A successful `Result<T>` containing `true` if the XML is valid; otherwise, a failure result containing structured error details.

    For high-throughput scenarios, prefer reusing streams where feasible, or use string buffers if the payload is already transcoded. While validating from a `ReadOnlySpan<T>` prevents payload array allocation via unmanaged memory pinning, the underlying XML reader implementation will still allocate internal decoding buffers on the Gen 0 heap. Exceptions: ArgumentNullException: `targetNamespace` is `null`; InvalidOperationException: The configured schema cache does not implement the schema set provider required for validation

  • methodValidate
    Result<bool> Validate(Stream xmlStream, string targetNamespace)

    Validates an XML stream against the precompiled schema associated with the specified namespace.

    xmlStream: Stream
    The readable stream containing the XML document data
    targetNamespace: string
    The target XML namespace of the schema to validate against

    Returns: A successful `Result<T>` containing `true` if the XML is valid; otherwise, a failure result containing structured error details.

    Exceptions: ArgumentNullException: `xmlStream` or `targetNamespace` is `null`; InvalidOperationException: The configured schema cache does not implement the schema set provider required for validation

  • methodValidate
    Result<bool> Validate(string xml, string targetNamespace)

    Validates an XML string against the precompiled schema associated with the specified namespace.

    xml: string
    The XML document string to validate
    targetNamespace: string
    The target XML namespace of the schema to validate against

    Returns: A successful `Result<T>` containing `true` if the XML is valid; otherwise, a failure result containing structured error details.

    Exceptions: ArgumentNullException: `xml` or `targetNamespace` is `null`; InvalidOperationException: The configured schema cache does not implement the schema set provider required for validation

  • methodValidateAsync
    Task<Result<bool>> ValidateAsync(Stream xmlStream, string targetNamespace, CancellationToken cancellationToken = default)

    Validates an XML stream asynchronously against the precompiled schema associated with the specified namespace.

    xmlStream: Stream
    The readable stream containing the XML document data
    targetNamespace: string
    The target XML namespace of the schema to validate against
    cancellationToken: CancellationToken
    A token that can be used to cancel the asynchronous operation

    Returns: A task representing the asynchronous operation. The task result contains a successful `Result<T>` with `true` if the XML is valid; otherwise, a failure result containing structured error details.

    Exceptions: ArgumentNullException: `xmlStream` or `targetNamespace` is `null`; OperationCanceledException: The operation was canceled via `cancellationToken`; InvalidOperationException: The configured schema cache does not implement the schema set provider required for validation

class

XmlSchemaCache

Assembly: EricksonLopez.Xml.Validation
public sealed class XmlSchemaCache : IXmlSchemaCache, IXmlSchemaSetProvider
Implements: IXmlSchemaCache, IXmlSchemaSetProvider

Provides a thread-safe cache for precompiled XSD schema sets with Anti-XXE protection.

Remarks:

All schema compilation operations prohibit DTD processing and disable external entity resolution to protect against XML External Entity (XXE) attacks and XML entity expansion vulnerabilities. Thread safety is guaranteed for concurrent registration and lookup operations.

Members (8)

  • constructorXmlSchemaCache
    public XmlSchemaCache()

    Initializes a new instance of the `XmlSchemaCache` class.

  • methodClear
    public void Clear()

    Removes all registered schemas from the cache.

    This operation is thread-safe. Concurrent registrations or lookups in progress will complete normally; subsequent operations will see an empty cache.

  • methodContainsSchema
    public bool ContainsSchema(string targetNamespace)

    Determines whether a schema for the specified target namespace is registered in the cache.

    targetNamespace: string
    The target XML namespace to look up

    Returns: `true` if a schema for the specified namespace is registered; otherwise, `false`.

    Exceptions: ArgumentNullException: `targetNamespace` is `null`

  • methodIsRootElementDeclared
    public bool IsRootElementDeclared(string targetNamespace, string localName, string namespaceUri)

    Determines whether the specified root element qualified name is declared as a global element in the registered schema.

    targetNamespace: string
    The target XML namespace of the schema
    localName: string
    The local name of the root element
    namespaceUri: string
    The namespace URI of the root element

    Returns: `true` if the element is declared as a global element in the target schema; otherwise, `false`.

    Exceptions: ArgumentNullException: `targetNamespace`, `localName`, or `namespaceUri` is `null`

  • methodRegisterSchema
    public unsafe void RegisterSchema(string targetNamespace, ReadOnlySpan<byte> utf8Xsd)

    Registers a precompiled XSD schema definition from the specified string.

    targetNamespace: string
    The target XML namespace associated with the schema
    utf8Xsd: ReadOnlySpan<byte>

    Exceptions: ArgumentNullException: `targetNamespace` or `xsdContent` is `null`; XmlException: The `xsdContent` is not well-formed XML; XmlSchemaException: The `xsdContent` contains schema structural or syntax errors

  • methodRegisterSchema
    public void RegisterSchema(string targetNamespace, Stream xsdStream)

    Registers a precompiled XSD schema definition from the specified string.

    targetNamespace: string
    The target XML namespace associated with the schema
    xsdStream: Stream

    Exceptions: ArgumentNullException: `targetNamespace` or `xsdContent` is `null`; XmlException: The `xsdContent` is not well-formed XML; XmlSchemaException: The `xsdContent` contains schema structural or syntax errors

  • methodRegisterSchema
    public void RegisterSchema(string targetNamespace, string xsdContent)

    Registers a precompiled XSD schema definition from the specified string.

    targetNamespace: string
    The target XML namespace associated with the schema
    xsdContent: string
    The raw XML Schema Definition (XSD) string to compile

    Exceptions: ArgumentNullException: `targetNamespace` or `xsdContent` is `null`; XmlException: The `xsdContent` is not well-formed XML; XmlSchemaException: The `xsdContent` contains schema structural or syntax errors

  • propertyCount
    public int Count { get; }

    Gets the total number of precompiled schema sets currently registered in the cache.

class

XmlSchemaCacheExtensions

Assembly: EricksonLopez.Xml.Validation
public static class XmlSchemaCacheExtensions

Provides extension methods for `IXmlSchemaCache` to support registration of XML schemas from the file system.

Members (2)

  • methodRegisterSchemaFile
    public static void RegisterSchemaFile(this IXmlSchemaCache cache, string targetNamespace, string filePath)

    Registers a precompiled XSD schema definition from a file on disk.

    cache: this IXmlSchemaCache
    The schema cache in which to register the schema
    targetNamespace: string
    The target XML namespace associated with the schema
    filePath: string
    The file path to the XSD schema file to compile and register

    Exceptions: ArgumentNullException: `cache`, `targetNamespace`, or `filePath` is `null`; FileNotFoundException: The file specified by `filePath` was not found; XmlException: The schema file does not contain well-formed XML; XmlSchemaException: The schema contains structural or syntax errors

  • methodRegisterSchemasFromDirectory
    public static int RegisterSchemasFromDirectory(this IXmlSchemaCache cache, string directoryPath, string searchPattern = "")

    Registers all matching XSD schema files discovered within the specified directory.

    cache: this IXmlSchemaCache
    The schema cache in which to register the schemas
    directoryPath: string
    The directory path containing XSD schema files
    searchPattern: string
    The search pattern used to match schema files. Defaults to "*.xsd".

    Returns: The number of schemas successfully compiled and registered. Returns `0` if no files match `searchPattern`.

    Optimized path (`XmlSchemaCache`): When `cache` is the built-in `XmlSchemaCache`, all XSD files that share the same `targetNamespace` are merged into a single compiled `Schema.XmlSchemaSet` before insertion. This ensures that schemas split across multiple files (e.g., via `xs:include`) are correctly aggregated into one cache entry per namespace. Fallback path (custom `IXmlSchemaCache` implementations): When `cache` is a custom implementation that does not extend `XmlSchemaCache`, a Liskov Substitution Principle (LSP) compatible fallback is used: each file is re-registered individually via `IXmlSchemaCache.RegisterSchema(string, Stream)`. If multiple files share the same `targetNamespace`, each call overwrites the previous entry (per ADR-003). Only the last file for a given namespace will be retained. Custom implementations of `IXmlSchemaCache` that need to support multi-file namespace aggregation must implement their own merge logic. Anti-XXE settings (`DtdProcessing.Prohibit`, `XmlResolver = null`) are enforced during all file reading and schema compilation operations in both paths. Exceptions: ArgumentNullException: `cache`, `directoryPath`, or `searchPattern` is `null`; DirectoryNotFoundException: The directory specified by `directoryPath` does not exist; XmlException: A schema file does not contain well-formed XML; XmlSchemaException: A schema file contains XML or schema compilation errors

class

XmlSchemaValidator

Assembly: EricksonLopez.Xml.Validation
public sealed partial class XmlSchemaValidator : IXmlSchemaValidator
Implements: IXmlSchemaValidator

Validates XML documents against precompiled XSD schemas while enforcing Anti-XXE protection and structured validation results.

Remarks:

Prohibits inline DTD processing and external entity resolution across all synchronous and asynchronous validation paths. Schema validation errors and warnings are captured without throwing runtime exceptions, returning a strongly-typed `Result<T>` indicating validation success or failure with diagnostic details.

Members (6)

  • constructorXmlSchemaValidator
    public XmlSchemaValidator(IXmlSchemaCache schemaCache)

    Initializes a new instance of the `XmlSchemaValidator` class using default validation options.

    schemaCache: IXmlSchemaCache
    The schema cache containing precompiled XSD schema sets

    The supplied `schemaCache` must implement the library's internal schema set provider (such as `XmlSchemaCache`). Exceptions: ArgumentNullException: `schemaCache` is `null`

  • constructorXmlSchemaValidator
    public XmlSchemaValidator(IXmlSchemaCache schemaCache, XmlValidationOptions options, ILogger<XmlSchemaValidator>? logger = null)

    Initializes a new instance of the `XmlSchemaValidator` class with custom validation options and diagnostic logging.

    schemaCache: IXmlSchemaCache
    The schema cache containing precompiled XSD schema sets
    options: XmlValidationOptions
    The configuration options controlling warning inclusion and severity
    logger: ILogger<XmlSchemaValidator>?
    The logger instance used for structured diagnostic logging. When `null`, a `Abstractions.NullLogger<T>` is substituted and no diagnostic output is emitted.

    The supplied `schemaCache` must implement the library's internal schema set provider (such as `XmlSchemaCache`). Exceptions: ArgumentNullException: `schemaCache` or `options` is `null`

  • methodValidate
    public unsafe Result<bool> Validate(ReadOnlySpan<byte> utf8Xml, string targetNamespace)

    Validates an XML string against the precompiled schema associated with the specified namespace.

    utf8Xml: ReadOnlySpan<byte>
    targetNamespace: string
    The target XML namespace of the schema to validate against

    Returns: A successful `Result<T>` containing `true` if the XML is valid; otherwise, a failure result containing structured error details.

    Exceptions: ArgumentNullException: `xml` or `targetNamespace` is `null`; InvalidOperationException: The configured schema cache does not implement the schema set provider required for validation

  • methodValidate
    public Result<bool> Validate(Stream xmlStream, string targetNamespace)

    Validates an XML string against the precompiled schema associated with the specified namespace.

    xmlStream: Stream
    targetNamespace: string
    The target XML namespace of the schema to validate against

    Returns: A successful `Result<T>` containing `true` if the XML is valid; otherwise, a failure result containing structured error details.

    Exceptions: ArgumentNullException: `xml` or `targetNamespace` is `null`; InvalidOperationException: The configured schema cache does not implement the schema set provider required for validation

  • methodValidate
    public Result<bool> Validate(string xml, string targetNamespace)

    Validates an XML string against the precompiled schema associated with the specified namespace.

    xml: string
    The XML document string to validate
    targetNamespace: string
    The target XML namespace of the schema to validate against

    Returns: A successful `Result<T>` containing `true` if the XML is valid; otherwise, a failure result containing structured error details.

    Exceptions: ArgumentNullException: `xml` or `targetNamespace` is `null`; InvalidOperationException: The configured schema cache does not implement the schema set provider required for validation

  • methodValidateAsync
    public async Task<Result<bool>> ValidateAsync(Stream xmlStream, string targetNamespace, CancellationToken cancellationToken = default)

    Validates an XML stream asynchronously against the precompiled schema associated with the specified namespace.

    xmlStream: Stream
    The readable stream containing the XML document data
    targetNamespace: string
    The target XML namespace of the schema to validate against
    cancellationToken: CancellationToken
    A token that can be used to cancel the asynchronous operation

    Returns: A task representing the asynchronous operation. The task result contains a successful `Result<T>` with `true` if the XML is valid; otherwise, a failure result containing structured error details.

    Exceptions: ArgumentNullException: `xmlStream` or `targetNamespace` is `null`; OperationCanceledException: The operation was canceled via `cancellationToken`; InvalidOperationException: The configured schema cache does not implement the schema set provider required for validation

class

XmlValidationOptions

Assembly: EricksonLopez.Xml.Validation
public sealed class XmlValidationOptions

Specifies configuration options for XML schema validation behavior.

Remarks:

Instances are configured when registering XML validation services via `XmlValidationServiceCollectionExtensions.AddXmlValidation(IServiceCollection, Action<XmlValidationOptions>)`.

Members (5)

  • propertyIncludeWarnings
    public bool IncludeWarnings { get; set; }

    Gets or sets a value indicating whether non-fatal XSD schema warnings are included in validation results.

    When `false` (default), only schema errors are reported unless `TreatWarningsAsErrors` is enabled. When `true`, warnings are captured and appended with the prefix `[Warning]`. If `TreatWarningsAsErrors` is enabled, this property always returns `true` to prevent silent warning drops.

  • propertyMaxCharactersInDocument
    public long MaxCharactersInDocument { get; set; }

    Gets or sets the maximum number of characters allowed in the XML document to defend against denial of service.

    Set to `0` for unlimited character count. Values must be greater than or equal to zero. Exceptions: ArgumentOutOfRangeException: `value` is less than zero

  • propertyMaxErrors
    public int MaxErrors { get; set; }

    Gets or sets the maximum number of validation errors and warnings collected before truncating further additions.

    Enforces an upper limit on collected diagnostic messages to defend against memory exhaustion on malformed inputs. Exceptions: ArgumentOutOfRangeException: `value` is less than or equal to zero

  • propertyProcessInlineSchema
    public bool ProcessInlineSchema { get; set; }

    Gets or sets a value indicating whether inline schemas encountered in the XML document are processed.

    The default is `false` to defend against inline schema poisoning on untrusted input. Enable only when processing documents with trusted inline schemas.

  • propertyTreatWarningsAsErrors
    public bool TreatWarningsAsErrors { get; set; }

    Gets or sets a value indicating whether XSD schema warnings are treated as validation errors.

    Setting this to `true` ensures schema warnings cause validation failure, and automatically includes warnings in the result.

class

XmlValidationServiceCollectionExtensions

Assembly: EricksonLopez.Xml.Validation
public static class XmlValidationServiceCollectionExtensions

Provides extension methods for `IServiceCollection` to register XML schema validation services and caching components.

Members (2)

  • methodAddXmlValidation
    public static IServiceCollection AddXmlValidation(this IServiceCollection services)

    Registers `IXmlSchemaCache` and `IXmlSchemaValidator` as singletons using default `XmlValidationOptions`.

    services: this IServiceCollection
    The service collection to register services into

    Returns: The `IServiceCollection` instance so that additional calls can be chained.

    Exceptions: ArgumentNullException: `services` is `null`

  • methodAddXmlValidation
    public static IServiceCollection AddXmlValidation(this IServiceCollection services, Action<XmlValidationOptions>? configure)

    Registers `IXmlSchemaCache` and `IXmlSchemaValidator` as singletons with configurable `XmlValidationOptions`.

    services: this IServiceCollection
    The service collection to register services into
    configure: Action<XmlValidationOptions>?
    An optional delegate to configure `XmlValidationOptions`. When `null`, options configured via `IOptions<TOptions>` or default options are used.

    Returns: The `IServiceCollection` instance so that additional calls can be chained.

    Exceptions: ArgumentNullException: `services` is `null` Example:

    services.AddXmlValidation(options =>
    {
        options.IncludeWarnings = true;
        options.TreatWarningsAsErrors = false;
    });

Search EricksonLopez.* Ecosystem

Type a library name, NuGet package ID, .NET type, or architectural concept to search across the portal.