Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 23, 2014 21:17
Show Gist options
  • Save Fhernd/c60f27be173608e90eed to your computer and use it in GitHub Desktop.
Save Fhernd/c60f27be173608e90eed to your computer and use it in GitHub Desktop.
Demostración de la validación con XSD en C#.
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
namespace Articulos.Preguntas
{
public sealed class ValidacionConXSD
{
public static void Main()
{
// Crea objeto para representar el esquema de los libros:
XmlSchemaSet sc = new XmlSchemaSet();
// Agrega el esquema `esquemaLibros.xsd` a la colección:
sc.Add ("urn:libreria-schema", "esquemaLibros.xsd");
// Aquí se especifica el tipo de validación que
// utilizaremos para el archivo XML:
XmlReaderSettings configXml = new XmlReaderSettings();
configXml.ValidationType = ValidationType.Schema;
configXml.Schemas = sc;
configXml.ValidationEventHandler += new ValidationEventHandler(ManejadorExcepcionesValidacion);
// Crea un objeto XmlReader:
XmlReader lectorXml = XmlReader.Create ("libros2.xml", configXml);
// Realiza parsing del archivo XML:
while (lectorXml.Read());
}
// Manejador de las excepcioens de validación:
private static void ManejadorExcepcionesValidacion(object sender, ValidationEventArgs e)
{
Console.WriteLine ("Error de validación XSD: {0}", e.Message.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment