Ir al contenido

Buscar en el Ecosistema EricksonLopez.*

Escribe el nombre de una librería, ID de paquete NuGet, tipo .NET o concepto arquitectónico para buscar en el portal.

Primeros Pasos con EricksonLopez.Xml.Validation

Instala EricksonLopez.Xml.Validation (1.0.0 en NuGet). Ten en cuenta que no existe un paquete raíz independiente EricksonLopez.Xml.

dotnet add package EricksonLopez.Xml.Validation --version 1.0.0

Registra esquemas en XmlSchemaCache, valida con XmlSchemaValidator y registra IXmlSchemaCache + IXmlSchemaValidator como singletons mediante AddXmlValidation:

using System;
using EricksonLopez.Result;
using EricksonLopez.Xml.Validation;
using Microsoft.Extensions.DependencyInjection;
namespace Example.XmlQuickStart;
public static class XmlUsage
{
public static void ValidateAndRegister()
{
const string ns = "https://example.com/orders";
const string xsd = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://example.com/orders"
elementFormDefault="qualified">
<xs:element name="Order">
<xs:complexType>
<xs:sequence>
<xs:element name="Id" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
""";
const string xml = """
<Order xmlns="https://example.com/orders">
<Id>ORD-100</Id>
</Order>
""";
var cache = new XmlSchemaCache();
cache.RegisterSchema(ns, xsd);
var validator = new XmlSchemaValidator(cache);
Result<bool> result = validator.Validate(xml, ns);
if (result.IsFailure || !result.Value)
{
throw new InvalidOperationException("Expected valid XML document.");
}
var services = new ServiceCollection();
services.AddLogging();
services.AddXmlValidation(options =>
{
options.IncludeWarnings = true;
options.MaxCharactersInDocument = 10_000_000;
});
using var provider = services.BuildServiceProvider(
new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true });
var diValidator = provider.GetRequiredService<IXmlSchemaValidator>();
if (diValidator is null)
{
throw new InvalidOperationException("IXmlSchemaValidator was not resolved.");
}
}
}