Mega Code Archive

 
Categories / C# Book / 06 XML
 

0552 Using XmlReader with XElement

using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Xml; using System.Xml.Linq; using System.Text; using System.IO; class Program { static void Main() { XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; using (XmlReader r = XmlReader.Create("foo.xml", settings)) { r.ReadStartElement("log"); while (r.Name == "logentry") { XElement logEntry = (XElement)XNode.ReadFrom(r); int id = (int)logEntry.Attribute("id"); DateTime date = (DateTime)logEntry.Element("date"); string source = (string)logEntry.Element("source"); } r.ReadEndElement(); } } }