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

Slides:



Advertisements
Similar presentations
TeSLa researchproduct. Why The Visual Basic Imports SySTem.STriNg Module Demo Public Sub Main() Dim S = Join(", ", { "Hello", "World"}) End Sub.
Advertisements

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
ISO DSDL ISO – Document Schema Definition Languages (DSDL) Martin Bryan Convenor, JTC1/SC18 WG1.
XML & Data Structures for the Internet Yingcai Xiao.
UFCEKG-20-2 Data, Schemas & Applications Lecture 5 XML & PHP.
XML: Managing Data Exchange Stylesheets. Lesson Contents CSS The basic XSL file XSL transforms Templates Sort Numbering Parameters and Variables Datatypes.
RDF: 1999 RDFS: 2004 SKOS first version: 2008 SPARQL 1.0: 2008 SPARQL 1.1: 2013 N3 first version: 2008 DAML started: 2000 OWL: OWL GeoSPARQL.
Joshua Clark Consultant Sogeti USA Blog:
Live from Redmond: WebCast Conquering XML with Visual Basic 9 Amanda Silver Lead Program Manager Visual Basic Level: Starts Easy, Finishes Advanced.
C# 3.0 & LINQ Raimond Brookman – IT Architect
Basic Elements of C++ Chapter 2.
Beth Massi Program Manager Visual Studio Community.
Paul Yuknewicz Lead Program Manager Microsoft Visual Basic DEV 319.
XML files (with LINQ). Introduction to LINQ ( Language Integrated Query ) C#’s new LINQ capabilities allow you to write query expressions that retrieve.
Programming in C# Language Overview
Introducing LINQ to XML Florin−Tudor Cristea, Microsoft Student Partner.
Chapter 12 Creating and Using XML Documents HTML5 AND CSS Seventh Edition.
JXON An Architecture for Schema and Annotation Driven JSON/XML Bidirectional Transformations David A. Lee Senior Principal Software Engineer Slide 1.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
HTML, XHTML, and CSS Chapter 12 Creating and Using XML Documents.
Dr. Azeddine Chikh IS446: Internet Software Development.
IBM Research © 2005 IBM Corporation XJ: Robust XML Processing in Java™ Mukund Raghavachari, Rajesh Bordawekar, Michael Burke, and Igor Peshansky IBM T.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 11 th Lecture Pavel Ježek
HeuristicLab. Motivation  less memory pressure no DOM single pass linear process  less developer effort no interfaces to implement  modularity & flexibility.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
Session IV Chapter 9 – XML Schemas
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
LINQ TO XML Mike Taulty Developer & Platform Group Microsoft UK
DOM Robin Burke ECT 360. Outline XHTML in Schema JavaScript DOM (MSXML) Loading/Parsing Transforming parameter passing DOM operations extracting data.
Avoid using attributes? Some of the problems using attributes: Attributes cannot contain multiple values (child elements can) Attributes are not easily.
Methods Session 04 Mata kuliah: M0874 – Programming II Tahun: 2010.
Extensible Stylesheet Language Chao-Hsien Chu, Ph.D. School of Information Sciences and Technology The Pennsylvania State University XSL-FO XSLT.
XML and Web Services CS409 Application Services Even Semester 2007.
Lecture 11 XSL Transformations (part 1: Introduction)
Лектор: Борислава Палева. Galin Iliev MCT, MCPD, MCSD.NET LINQ to XML.
LINQ and C# 3.0 Mads Torgersen Program Manager for the C# Language Microsoft Corporation.
LINQ: It’s Not Your Father’s Data Access Denny Boynton Anheuser-Busch Companies.
The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation.
Freemarker ● Introduction ● Core features ● Java part example ● Template example ● Expressions ● Builtins ● Assigning value ● Conditions ● Loops ● Macros.
Advanced PHP & RSS Utilizing XML, RSS, and PHP. XML (eXtensible Markup Language) XML is the language of all RSS feeds and subscriptions XML is basically.
VB and C# Programming Basics. Overview Basic operations String processing Date processing Control structures Functions and subroutines.
1 Introduction  Extensible Markup Language (XML) –Uses tags to describe the structure of a document –Simplifies the process of sharing information –Extensible.
WPI, MOHAMED ELTABAKH PROCESSING AND QUERYING XML 1.
Tutorial 13 Validating Documents with Schemas
XML. DCS – SWC 2 Data vs. Information We often use the terms data and information interchangeably More precisely, data is some ”value” of a certain type,
Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann
Unit 3 — Advanced Internet Technologies Lesson 11 — Introduction to XSL.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
XML. RHS – SOC 2 Data vs. Information We often use the terms data and information interchangeably More precisely, data is some ”value” of a certain type,
Ken Casada Developer Evangelist Microsoft Switzerland
V 1.0 OE-NIK HP 1 Advanced Programming LINQ approach XML handling, XLINQ Exercise.
ACG 4401 XSLT Extensible Stylesheet Language for Transformations Presenting XML and XBRL.
XML Extensible Markup Language
XML DOM Week 11 Web site:
XML Schema – XSLT Week 8 Web site:
1 Using an XML Parser. 2 Objective You will be able to use a publically available open source parser to convert an XML file into an internal data structure.
1 XSLT XSLT (extensible stylesheet language – transforms ) is another language to process XML documents. Originally intended as a presentation language:
Unit 4 Representing Web Data: XML
Parsing XML XDocument and LINQ
Introduction to the Document Object Model
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Classes In C#.
Chapter 7 Representing Web Data: XML
Processing XML.
XML Data Introduction, Well-formed XML.
Fundamentals of Functional Programming
04 | Data Acess Technologies
Presentation transcript:

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

LINQ to XML – Example XML Patrick Hines Main St Mercer Island WA Gretchen Rivas Main St Mercer Island WA Scott MacDonald Stewart St Chatsworth CA Pavel Ježek LINQ to XML

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 = " "; XmlElement phone2 = doc.CreateElement("phone"); phone2.SetAttribute("type", "work"); phone2.InnerText = " "; 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

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", " ", new XAttribute("type", "home")), new XElement("phone", " ", 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", " "), 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 …

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 “ namespace: XElement contacts = new XElement("{ …); Instead of explicit alias support, existing language facilities should be used. Typical pattern: string myNs = "{ XElement contacts = new XElement(myNs+"contacts", new XElement(myNs+"contact", new XElement(myNs+"name", "Patrick Hines"), new XElement(myNs+"phone", " ", new XAttribute("type", "home")), ) ) ); Results in following XML: Patrick Hines Pavel Ježek C# 3.0 and.NET 3.5

LINQ to XML – Loading Existing XML Data From string: XElement contacts = Patrick Hines Main St Mercer Island WA "); From file: XElement contactsFromFile = Pavel Ježek C# 3.0 and.NET 3.5

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

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[] {" ", " "} }, new Person { Name="Gretchen Rivas", PhoneNumbers = new string[] {" "} } }; 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 Gretchen Rivas

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

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: some text EMPHASED TEXT other text with newline Output: some textEMPHASED TEXTother text with newline

LINQ to XML – Traversing XML XML data: Met in Patrick Hines 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 Patrick Hines Patrick Hines

LINQ to XML – Manipulating XML Code samples: XElement mobilePhone = new XElement("phone", " "); 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", " "); 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", " "); contact.SetElement(“phone", null); Pavel Ježek C# 3.0 and.NET 3.5

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 WA Gretchen Rivas WA Scott MacDonald Pavel Ježek C# 3.0 and.NET 3.5 Patrick Hines Main St Mercer Island WA Gretchen Rivas Main St Mercer Island WA Scott MacDonald Stewart St Chatsworth CA

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