Presentation is loading. Please wait.

Presentation is loading. Please wait.

Лектор: Борислава Палева. Galin Iliev MCT, MCPD, MCSD.NET LINQ to XML.

Similar presentations


Presentation on theme: "Лектор: Борислава Палева. Galin Iliev MCT, MCPD, MCSD.NET LINQ to XML."— Presentation transcript:

1 лектор: Борислава Палева

2 Galin Iliev MCT, MCPD, MCSD.NET http://www.galcho.com LINQ to XML

3 Agenda LINQ Architecture LINQ to XML ( vs. DOM) –Create –Traverse –Transform LINQ to XSD

4 LINQ Architecture Objects XML Relational LINQ enabled data sourcesLINQ To Objects LINQ To XML LINQ enabled ADO.NET VBOthers… LINQ To Entities LINQ To SQL LINQ To Datasets.Net Language Integrated Query (LINQ) C#

5 LINQ to XML (XLINQ) Declarative construction of XML document Support for language integrated queries Cleaner, simpler, smaller and faster XML API

6 XML using DOM XmlDocument doc = new XmlDocument(); XmlElement contacts = doc.CreateElement("contacts"); foreach (Customer c in customers) if (c.Country == "USA") { XmlElement e = doc.CreateElement("contact"); XmlElement name = doc.CreateElement("name"); name.InnerText = c.CompanyName; e.AppendChild(name); XmlElement phone = doc.CreateElement("phone"); phone.InnerText = c.Phone; e.AppendChild(phone); contacts.AppendChild(e); } doc.AppendChild(contacts); Programming XML today Great Lakes Food (503) 555-7123 … Imperative model Document centric No integrated queries Memory intensive

7 LINQ to XML XElement contacts = new XElement("contacts", from c in customers where c.Country == "USA" select new XElement("contact", new XElement("name", c.CompanyName), new XElement("phone", c.Phone) ) ); Programming XML with LINQ Declarative model Element centric Integrated queries Smaller and faster

8 LINQ to XML (XElement) vs. DOM (XmlElement) XElement needn’t be part of a “document” object –An XDocument can be created if needed, but XElements can exist without it XElement.Name property doesn’t include a namespace prefixes –LINQ to XML resolves namespaces and prefixes at serialization time Leaf elements can be cast directly into data int age = (int?)xAge; //LINQ to XML int age = (int?)xAge.InnerText; //DOM XElement.ToString() outputs the underlying XML

9 LINQ Query on LINQ to XML XElement doc = XElement.Load("customers.xml"); var query = from c in doc.Element("Root").Elements("Customers") where c.Element("FullAddress“).Element("Country").Value == "Germany" select c; foreach (XElement result in query) Console.WriteLine(result);

10 Agenda LINQ Architecture LINQ to XML (XLINQ) XML using DOM LINQ to XML LINQ to XML (XElement) vs. DOM (XmlElement) LINQ Query on LINQ to XML DEMO Transforms with DOM Transforms with LINQ to XML Navigation primitives VB 9.0 Literals LINQ to XSD DEMO Q&A

11 Transforms with DOM //Load the stylesheet. XslTransform xslt = new XslTransform(); xslt.Load(stylesheet); //Load the file to transform. XPathDocument doc = new XPathDocument(filename); //Create an XmlTextWriter which outputs to the console. XmlTextWriter writer = new XmlTextWriter(Console.Out); //Transform the file and send the output to the console. xslt.Transform(doc, null, writer, null); writer.Close();

12 Transforms with LINQ to XML XElement element = new XElement("salaried_employees", from e in doc.Descendants("employee") where e.Attribute("salaried").Value == "true" select new XElement("employee", new XElement(e.Element("name")) ) );

13 Navigation primitives in LINQ to XML Similar to the path axes in XPath 1.0 –Nodes() : retrieves all the children –Elements(): retrieves all elements children –Elements(“name”): selects children elem. by name –Attributes() –Parent() –Descendants() –Etc

14 LINQ to XML Enhancement for VB Only: XML Literals VB can create XElements in memory with XElement constructors, same as in C# Dim contacts As XElement = _ New XElement("contacts", _ New XElement("contact", _ New XElement("name", “Fred Blogs"), _ 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", "98040"))))

15 Creating an XElement from an XML Literal Wouldn’t you rather do this? Dim contacts As XElement = _ Fred Blogs 206-555-0144 425-555-0145 123 Main St Mercer Island WA 98040 Compiler converts this to same code as previous slide No need for line continuation characters within the XML

16 VB XML Literals Can Contain Expressions Evaluated at Runtime Element Value from Variable Dim name As XElement = Element Value from Function Call Dim name As XElement = Attribute Value from Variable Dim phone as XElement = 234-5678 Element Name from Variable Dim contact as XElement = Foo

17 Embedding LINQ to XML Query Inside XML Literal Dim root As XElement = _ <%= From p In dc.Products _ Select > %>

18 LINQ to XML Enhancement for VB Only: Late Bound XML VB can apply three XPath-style navigational operators to an XElement –Child axis operator: “.” –Descendants axis operator: “…” –Attribute axis operator: “@” This makes the syntax simpler than the index-based XElement methods –However, VB applies these operators at runtime using late binding, so their use is not type-safe

19 Late Bound XML Example For Each Dim phone In contact.phone Console.WriteLine(CStr(phone.@type))phone.@type) Next Console.WriteLine(CStr(contacts... (0))) … which is equivalent to For Each Dim phone In Contact.Element("phone") Console.WriteLine(CStr(phone.Attribute("type")) Next Console.WriteLine(CStr(contact.Descendants("city")(0))

20 Schema-Aware LINQ to XML? Current LINQ to XML release are unaware of XML structure at compile time –Nodes must be located at run time using indexes, and values must be cast to correct data type string zip = (string)name.Element(“zip”) Microsoft is working toward to construct “strongly typed” XML objects string zip = name.zip LINQ to XSD – alpha v0.2

21 Agenda LINQ Architecture LINQ to XML (XLINQ) XML using DOM LINQ to XML LINQ to XML (XElement) vs. DOM (XmlElement) LINQ Query on LINQ to XML DEMO Transforms with DOM Transforms with LINQ to XML Navigation primitives VB 9.0 Literals LINQ to XSD DEMO Q&A

22 Resources.NET Language-Integrated Query for XML Data Microsoft XML Team's WebLog LINQ Tutorial (In Bulgarian) LINQ Project Forum

23 Q&A

24


Download ppt "Лектор: Борислава Палева. Galin Iliev MCT, MCPD, MCSD.NET LINQ to XML."

Similar presentations


Ads by Google