Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics LINQ to XML: A Brief Overview Pavel Ježek.

Similar presentations


Presentation on theme: "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics LINQ to XML: A Brief Overview Pavel Ježek."— Presentation transcript:

1 CHARLES UNIVERSITY IN PRAGUE http://d3s.mff.cuni.cz faculty of mathematics and physics LINQ to XML: A Brief Overview Pavel Ježek

2 LINQ to XML – Example XML Patrick Hines 206-555-0144 425-555-0145 123 Main St Mercer Island WA 68042 10 Gretchen Rivas 206-555-0163 123 Main St Mercer Island WA 68042 11 Scott MacDonald 925-555-0134 425-555-0177 345 Stewart St Chatsworth CA 91746 500000 Pavel Ježek LINQ to XML

3 LINQ to XML – Creating XML via DOM (without XLinq) XmlDocument doc = new XmlDocument(); XmlElement name = doc.CreateElement("name"); name.InnerText = "Patrick Hines"; XmlElement phone1 = doc.CreateElement("phone"); phone1.SetAttribute("type", "home"); phone1.InnerText = "206-555-0144"; XmlElement phone2 = doc.CreateElement("phone"); phone2.SetAttribute("type", "work"); phone2.InnerText = "425-555-0145"; XmlElement street1 = doc.CreateElement("street1"); street1.InnerText = "123 Main St"; XmlElement city = doc.CreateElement("city"); city.InnerText = "Mercer Island"; XmlElement state = doc.CreateElement("state"); state.InnerText = "WA"; XmlElement postal = doc.CreateElement("postal"); postal.InnerText = "68042"; XmlElement address = doc.CreateElement("address"); address.AppendChild(street1); address.AppendChild(city); address.AppendChild(state); address.AppendChild(postal); XmlElement contact = doc.CreateElement("contact"); contact.AppendChild(name); contact.AppendChild(phone1); contact.AppendChild(phone2); contact.AppendChild(address); XmlElement contacts = doc.CreateElement("contacts"); contacts.AppendChild(contact); doc.AppendChild(contacts); Pavel Ježek C# 3.0 and.NET 3.5

4 LINQ to XML – Creating XML via XLinq Functional construction: XElement contacts = new XElement("contacts", new XElement("contact", new XElement("name", "Patrick Hines"), new XElement("phone", "206-555-0144", new XAttribute("type", "home")), new XElement("phone", "425-555-0145", new XAttribute("type", "work")), new XElement("address", new XElement("street1", "123 Main St"), new XElement("city", "Mercer Island"), new XElement("state", "WA"), new XElement("postal", "68042") ) ) ); Imlicitly no XML document Can be added if some processing information is needed: XDocument contactsDoc = new XDocument( new XDeclaration("1.0", "UTF-8", "yes"), new XComment("XLinq Contacts XML Example"), new XProcessingInstruction("MyApp", "123-44-4444"), new XElement("contacts", new XElement("contact", new XElement("name", "Patrick Hines"), … Pavel Ježek C# 3.0 and.NET 3.5 Resulting XML: Patrick Hines …

5 LINQ to XML – XML Namespaces There is a support for XML namespaces, but namespace aliases are always expanded into full names Creating element from a “http://myCompany.com” namespace: XElement contacts = new XElement("{http://myCompany.com}contacts", …); Instead of explicit alias support, existing language facilities should be used. Typical pattern: string myNs = "{http://mycompany.com}"; XElement contacts = new XElement(myNs+"contacts", new XElement(myNs+"contact", new XElement(myNs+"name", "Patrick Hines"), new XElement(myNs+"phone", "206-555-0144", new XAttribute("type", "home")), ) ) ); Results in following XML: Patrick Hines 206-555-0144 Pavel Ježek C# 3.0 and.NET 3.5

6 LINQ to XML – Loading Existing XML Data From string: XElement contacts = XElement.Parse( @" Patrick Hines 206-555-0144 425-555-0145 123 Main St Mercer Island WA 68042 10 "); From file: XElement contactsFromFile = XElement.Load(@"c:\myContactList.xml"); Pavel Ježek C# 3.0 and.NET 3.5

7 LINQ to XML – Creating XElements XElement’s constructor: public XElement(XName name, params object[] contents) Any of the parameters passed to contents can be: 1. A string, which is added as text content 2. An XElement, which is added as a child element 3. An XAttribute, which is added as an attribute 4. An XProcessingInstruction, XComment, or XCData, which is added as child content 5. An IEnumerable, which is enumerated, and these rules are applied recursively 6. Anything else, ToString() is called and the result is added as text content 7. null, which is ignored Pavel Ježek C# 3.0 and.NET 3.5

8 LINQ to XML – Adding IEnumerables to XElement class Person { public string Name; public string[] PhoneNumbers; } var persons = new [] { new Person { Name="Patrick Hines", PhoneNumbers = new string[] {"206-555-0144", "425-555-0145"} }, new Person { Name="Gretchen Rivas", PhoneNumbers = new string[] {"206-555-0163"} } }; Following code using Standard Query Operators on IEnumerable can be used to transform a datastructure to XML: XElement contacts = new XElement("contacts", from p in persons select new XElement("contact", new XElement("name", p.Name), from ph in p.PhoneNumbers select new XElement("phone", ph) ) ); Console.WriteLine(contacts); Pavel Ježek C# 3.0 and.NET 3.5 Output: Patrick Hines 206-555-0144 425-555-0145 Gretchen Rivas 206-555-0163

9 LINQ to XML – Element Text as Value (1) XElement (text inside XElement) can be explicitly converted to a value: XElement name = new XElement("name", "Patrick Hines"); string nameString = (string) name; Console.WriteLine(nameString); ToString() returns the XML representation of the whole element: string nameString = name.ToString(); Console.WriteLine(nameString); Explicit type operators are provided for following types: string, bool, bool?, int, int?, uint, uint?, long, long?, ulong, ulong?, float, float?, double, double?, decimal, decimal?, DateTime, DateTime?, TimeSpan, TimeSpan?, GUID, GUID? Pavel Ježek C# 3.0 and.NET 3.5 Output: Patrick Hines Output: Patrick Hines

10 LINQ to XML – Element Text as Value (2) All element text is merged together: XElement xhtml = new XElement(“body", 1234, new XElement("br", null), 5678, "some text", new XElement("br", ""), new XElement("em", "EMPHASED TEXT"), "other text\nwith newline" ); Console.WriteLine(nameString); Console.WriteLine((string) nameString); Pavel Ježek C# 3.0 and.NET 3.5 Output: 1234 5678some text EMPHASED TEXT other text with newline Output: 12345678some textEMPHASED TEXTother text with newline

11 LINQ to XML – Traversing XML XML data: Met in 2005. Patrick Hines 206-555-0144 425-555-0145 Code examples: foreach (c in contact.Content()) { Console.WriteLine(c); } foreach (c in contact.Content ()) { Console.WriteLine(c) } foreach (x in contact.Elements()) { Console.WriteLine(x); } foreach (x in contact.Elements("phone")) { Console.WriteLine(x); } XElement name = contact.Element("name"); string name = (string) contact.Element("name"); Pavel Ježek C# 3.0 and.NET 3.5 Met in 2005. Patrick Hines 206-555-0144 425-555-0145 Patrick Hines 206-555-0144 425-555-0145 206-555-0144 425-555-0145

12 LINQ to XML – Manipulating XML Code samples: XElement mobilePhone = new XElement("phone", "206-555-0168"); contact.Add(mobilePhone); contact.AddFirst(mobilePhone); contact.Add(new XElement("address", new XElement("street", "123 Main St"), new XElement("city", "Mercer Island"), new XElement("state", "WA"), new XElement("country", "USA"), new XElement("postalCode", "68042") )); XElement mobilePhone = new XElement("phone", "206-555-0168"); XElement firstPhone = contact.Element("phone"); firstPhone.AddAfterThis(mobilePhone); contact.Element("phone").Remove(); contact.Elements("phone").Remove(); contact.Element("address").ReplaceContent(new XElement("street", "123 Brown Lane"), new XElement("city", "Redmond"), new XElement("state", "WA"), new XElement("country", "USA"), new XElement("postalCode", "68072")); contact.SetElement("phone", "425-555-0155"); contact.SetElement(“phone", null); Pavel Ježek C# 3.0 and.NET 3.5

13 LINQ to XML - Queries Example – flattening contacts: new XElement("contacts", from c in contacts.Elements("contact") select new object[] { new XComment("contact"), new XElement("name", (string)c.Element("name")), c.Elements("phone"), new XElement("address", c.Element("address")) } ); Results in: Patrick Hines 206-555-0144 425-555-0145 WA Gretchen Rivas WA Scott MacDonald 925-555-0134 425-555-0177... Pavel Ježek C# 3.0 and.NET 3.5 Patrick Hines 206-555-0144 425-555-0145 123 Main St Mercer Island WA 68042 10 Gretchen Rivas 206-555-0163 123 Main St Mercer Island WA 68042 11 Scott MacDonald 925-555-0134 425-555-0177 345 Stewart St Chatsworth CA 91746 500000

14 LINQ to XSD – Schema Aware programming Without schema: public static double GetTotalByZip(XElement root, int zip) { return ( from order in root.Elements("order"), item in order.Elements("item"), where (int) order.Element("address").Element("postal") == zip select ((double) price * (int) qty) ).Sum(); } With schema (1.0 downloadable from CodePlex): public static double GetTotalByZip(Orders root, int zip) { return ( from order in root.OrderCollection, item in order.Items where order.Address.Postal == zip select (item.Price * item.Quantity) ).Sum(); } Pavel Ježek C# 3.0 and.NET 3.5


Download ppt "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics LINQ to XML: A Brief Overview Pavel Ježek."

Similar presentations


Ads by Google