martes, 1 de julio de 2008

Lectura de archivos XML en .NET con C#

Siguiendo con el desarrollo de mi proyecto fin de master, me he encontrado con la necesidad de saber el contenido de ciertos archivos XML, llamados KML. En mi caso, principalmente necesito saber las coordenadas de un punto de interés que este archivo guardará.

En .NET es relativamente sencillo realizar un pequeño parser para la lectura y evaluación de un archivo XML. Lo primero que se debe hacer es añadir el namespace System.XML a la solución del proyecto. Este namespace contiene todas las clases y funciones para la lectura del fichero.

Imaginemos un fichero XML muy esquemático como el siguiente:

<sitios>
<lugar>
<nombre>Edificio Valdés Salas</nombre>
<coordenadas>43.36176418283314,-5.846278832424399</coordenadas>
</lugar>
<lugar>
<nombre>Plaza de América</nombre>
<coordenadas>43.3944736223344,-5.6649383733772823</coordenadas>
</lugar>
</sitios>

Para la lectura de este fichero XML, el parser utilizado podría ser el siguiente:


using System.XML;
namespace ParserXML
{
Class Program
{
Static void main(String[] args)
{
XMLDocument documentoXML = new XMLDocument();
documentoXML.Load("fichero.xml");
XmlNodeList sitios = documentoXML.GetElementsByTagName("Sitios");
XmlNodeList listaSitios = ((XmlElement)sitios[0]).GetElementsByTagName("Lugar");
foreach(XmlElement nodoLista in listaSitios)
{
int i = 0;
XmlNodeList coordenadas = nodoLista.GetElementsByTagName("Coordenadas");
Console.WriteLine(coordenadas[i].InnerText);
}
}
}
}

2 comentarios:

Anónimo dijo...

It isn't hard at all to start making money online in the underground world of [URL=http://www.www.blackhatmoneymaker.com]blackhat make money[/URL], It's not a big surprise if you have no clue about blackhat marketing. Blackhat marketing uses little-known or not-so-known avenues to produce an income online.

Anónimo dijo...

[url=http://www.realcazinoz.com]casino[/url], also known as accepted casinos or Internet casinos, are online versions of memorable ("buddy and mortar") casinos. Online casinos approve gamblers to possession some in and wager on casino games property of the Internet.
Online casinos typically nominate up respecting readily obtainable odds and payback percentages that are comparable to land-based casinos. Some online casinos contend higher payback percentages looking inasmuch as m‚range defender games, and some permute known payout border audits on their websites. Assuming that the online casino is using an correctly programmed indefinitely clique generator, pr‚cis games like blackjack enquire an established congress edge. The payout rate after these games are established ethical valued days the rules of the game.
Uncountable online casinos sublease or acquire their software from companies like Microgaming, Realtime Gaming, Playtech, Worldwide Cunning Technology and CryptoLogic Inc.