Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 23, 2014 23:49
Show Gist options
  • Save Fhernd/8bd7f3095532076c382c to your computer and use it in GitHub Desktop.
Save Fhernd/8bd7f3095532076c382c to your computer and use it in GitHub Desktop.
Conversión a tipos de la CLR desde el documento XML.
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
namespace Articulos.Preguntas
{
public sealed class LecturaAtributosXML
{
public static void Main()
{
// Configuración XML para el lector:
XmlReaderSettings configXml = new XmlReaderSettings();
configXml.ValidationType = ValidationType.Schema;
configXml.Schemas.Add ("urn:contrato-empleado", "FechaContrato.xsd");
XmlReader lectorXml = XmlReader.Create ("FechaContrato.xml", configXml);
// Despaza el lector a la primera posición con contenido
// en el documento XML:
lectorXml.MoveToContent();
// Lee el nodo anidado `fecha-contrato`:
lectorXml.ReadToDescendant("fecha-contrato");
// `fecha-contrato` como una instancia de DateTime:
DateTime fechaContrato = lectorXml.ReadElementContentAsDateTime();
Console.WriteLine ("Fecha después de seis meses del contrato: {0}", fechaContrato.AddMonths(6));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment