XML reader

String xml = new WebClient().DownloadString(@"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"); XDocument doc = XDocument.Parse(xml); DataTable dt = new DataTable(); dt.Columns.Add("Moneda", typeof(string)); dt.Columns.Add("Valor", typeof(float)); foreach (XElement element in doc.Descendants()) { if (!String.IsNullOrEmpty((String)element.Attribute("currency"))) { String moneda = element.Attribute("currency").Value; dt.Rows.Add(moneda, element.Attribute("rate").Value.ToStrim().Replace(".", ",")); } } Another example using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Text; using System.Xml; using System.IO; namespace Utilidades { static class Program { /// <summary> /// Punto de entrada principal para la aplicación. /// </summary> [STAThread] static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); StringBuilder output = new StringBuilder(); String xmlString = @"<bookstore> <book genre='autobiography' publicationdate='1981-03-22' ISBN='1-861003-11-0'> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> </bookstore>"; // Create an XmlReader using (XmlReader reader = XmlReader.Create(new StringReader(xmlString))) { reader.ReadToFollowing("book"); reader.MoveToFirstAttribute(); string genre = reader.Value; output.AppendLine("The genre value: " + genre); reader.MoveToNextAttribute(); string publicationDate = reader.Value; output.AppendLine("The date value: " + publicationDate); reader.ReadToFollowing("title"); output.AppendLine("Content of the title element: " + reader.ReadElementContentAsString()); } Console.Write(output.ToString()); } } }
Snippet to read a downloaded xml

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.