Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 23, 2014 22:44
Show Gist options
  • Save Fhernd/f6b9e984171f75e6ae5e to your computer and use it in GitHub Desktop.
Save Fhernd/f6b9e984171f75e6ae5e to your computer and use it in GitHub Desktop.
Lectura de los nodos de un archivo XML con XmlReader. En C#.
using System;
using System.Xml;
using System.IO;
namespace Articulos.Preguntas
{
public sealed class LecturaElementosXML
{
public static void Main()
{
XmlReader lectorXml = XmlReader.Create ("libros.xml");
// Este método obvia los nodos que no son parte del contenido, y pasa
// directamente a nodos de contenido:
while (lectorXml.Read())
{
if (lectorXml.IsStartElement())
{
Console.WriteLine ("<{0}> ", lectorXml.Name);
}
else
{
Console.Write ("<{0}> ", lectorXml.Name);
// Lee el inicio de la marca:
lectorXml.Read();
// Lectura de elementos anidados:
if (lectorXml.IsStartElement())
{
Console.Write ("\t<{0}>", lectorXml.Name);
}
// Lee el texto del elemento:
Console.WriteLine (lectorXml.ReadString());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment