© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelLINQ via C# 3.0Chapter 5 – LINQ to XML © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelLINQ to XML•Introducing a light-weight XML object model: XElement•Generating XML fragments with the XElement API•Querying XElement DOMs © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelPrimary LINQ to XML Types•LINQ to XML is implemented in System.Xml.Linq.dll•Primary classes:–XElement – can contain a document or a fragment–XDocument – full-fledged XML document (with declaration, processing instructions etc.)–XAttribute – represents an XML attribute•In-memory XmlReader-based model © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelConstructing XML•Construction statements closely mirror the XML structure:XElement library = new XElement("library", new XElement("book", new XAttribute("id", "1234"), new XElement("author", "Juwal Lowy"), new XElement("title", "Programming WCF")), new XElement("book", new XAttribute("id", "5678"), new XElement("author", "Jeffrey Richter"), new XElement("title", "CLR via C# 2.0")));Console.WriteLine(library);<library> <book id="1234"> <author>Juwal Lowy</author> <title>Programming WCF</title> </book> <book id="5678"> <author>Jeffrey Richter</author> <title>CLR via C# 2.0</title> </book></library> © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelConstructing XML from a Query•Various LINQ sources working together:XElement catalog = new XElement("students", (from student in students select new XElement("student", new XAttribute("name", student.Name), new XAttribute("id", student.Id), new XElement("grades", from grade in student.Grades select new XElement("grade", grade) ) ) ).Take(3));<students> <student name="John" id="1"> <grades> <grade>78</grade> <grade>89</grade> <grade>84</grade> </grades> </student> <student . /></students> © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelLoading and Saving XML•XElements can be loaded from and saved to a variety of sources•XElement.Save•XElement.Load•XElement.Parse•XElement.ToString © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelQuerying XML•The LINQ to XML query model relies on LINQ to Objects–XElement and friends implement IEnumerablevar averages = from student in catalog.Elements("student") select new { Name = student.Attribute("name").Value, Average = (from grade in student.Element("grades") .Elements("grade") select int.Parse(grade.Value)).Average() };foreach (var student in averages) Console.WriteLine(student.Name + ": " + student.Average); © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelAxis Methods•Additional axis methods to assist with traversing the XML tree:int[] stats = new int[2]; catalog.Descendants("grade").Aggregate(stats, (s, g) => { ++s[0]; s[1] += int.Parse(g.Value); return s; });Console.WriteLine("{0} scores, average: {1}", stats[0], stats[1] / stats[0]);//More methods://Ancestors, IsBefore, IsAfter, Nodes, Attributes, Elements © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelWorking with Namespaces•Namespaces in DOM have always been tedious–XmlNamespaceManager–Remembering to add nsName: before each operation on the DOM•LINQ to XML introduces the XName and XNamespace classes, implicitly convertible to and from string © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelNamespaces in LINQ to XMLXNamespace courseNs = "http://www.example.com/Courses/2008";XNamespace durationNs = "http://www.example.com/Duration";XElement courses = new XElement(courseNs + "courses", new XElement(courseNs + "course", new XElement(courseNs + "title", "LINQ via C# 3.0"), new XElement(durationNs + "duration", "2")), new XElement(courseNs + "course", new XElement(courseNs + "title", "Silverlight 2.0"), new XElement(durationNs + "duration", "2")));var totalDuration = (from duration in courses.Descendants(durationNs + "duration") select int.Parse(duration.Value)).Sum(); [...].. .LINQ to XML Performance 1 Preatomize XName and XNamespace objects 2 Prefer LINQ to XML (statically resolved) instead of XPath (runtime) 3 Use XmlReader for performance-critical, read-only, forward-only paths LINQ to XML is faster than XmlDocument but slower than XmlReader http://tinyurl.com/65e5u6 © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51 202 Israel Chapter. .. Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51 202 Israel Chapter Summary • XElement simplifies working with XML • Generating XML fragments with the XElement API • Querying XElement DOMs © Copyright SELA Software & Education Labs Ltd 1 4-1 8 Baruch Hirsch St Bnei Brak 51 202 Israel . Education Labs Ltd. 1 4-1 8 Baruch Hirsch St. Bnei Brak 51 202 IsraelPrimary LINQ to XML Types LINQ to XML is implemented in System .Xml .Linq. dll•Primary classes:–XElement. Use XmlReader for performance-critical, read-only, forward-only pathsLINQ to XML is faster than XmlDocument but slower than XmlReaderhttp://tinyurl.com/65e5u6 ©