Advertisement
Fhernd

ValidacionConXSD.cs

Jul 22nd, 2014
1,667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2. using System.Xml;
  3. using System.Xml.Schema;
  4. using System.IO;
  5.  
  6. namespace Articulos.Preguntas
  7. {
  8.     public sealed class ValidacionConXSD
  9.     {
  10.         public static void Main()
  11.         {
  12.             // Crea objeto para representar el esquema de los libros:
  13.             XmlSchemaSet sc = new XmlSchemaSet();
  14.            
  15.             // Agrega el esquema `esquemaLibros.xsd` a la colección:
  16.             sc.Add ("urn:libreria-schema", "esquemaLibros.xsd");
  17.            
  18.             // Aquí se especifica el tipo de validación que
  19.             // utilizaremos para el archivo XML:
  20.             XmlReaderSettings configXml = new XmlReaderSettings();
  21.             configXml.ValidationType = ValidationType.Schema;
  22.             configXml.Schemas = sc;
  23.             configXml.ValidationEventHandler += new ValidationEventHandler(ManejadorExcepcionesValidacion);
  24.            
  25.             // Crea un objeto XmlReader:
  26.             XmlReader lectorXml = XmlReader.Create ("libros2.xml", configXml);
  27.            
  28.             // Realiza parsing del archivo XML:
  29.             while (lectorXml.Read());
  30.         }
  31.        
  32.         // Manejador de las excepcioens de validación:
  33.         private static void ManejadorExcepcionesValidacion(object sender, ValidationEventArgs e)
  34.         {
  35.             Console.WriteLine ("Error de validación XSD: {0}", e.Message.ToString());
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement