linq to xml

30
1 LINQ to XML Mike Taulty Developer & Platform Group Microsoft Ltd [email protected] http://www.miketaulty.com

Upload: ukdpe

Post on 10-May-2015

2.981 views

Category:

Technology


0 download

DESCRIPTION

LINQ to XML

TRANSCRIPT

Page 1: LINQ to XML

1

LINQ to XML

Mike TaultyDeveloper & Platform GroupMicrosoft [email protected] http://www.miketaulty.com

Page 2: LINQ to XML

Agenda

Do we need LINQ to XML?Can we make a better XML API?

Tour of LINQ to XMLCreating, Querying, Modifying

Possible FuturesThe “LINQ to XSD” Alpha Preview

If Time Allows...Working with Schema, XPath and Large DocumentsVisual Basic

Page 3: LINQ to XML
Page 4: LINQ to XML

4

A better XML API?

Page 5: LINQ to XML

LINQ to XML – Basic Facts

An XML API implemented in assemblySystem.Xml.Linq.dll

NamespacesSystem.Xml.LinqSystem.Xml.SchemaSystem.Xml.XPath

Integrates with Language INtegrated QueryReleased with .NET Framework V3.5 in Visual Studio 2008

Page 6: LINQ to XML

Key Classes in System.Xml.Linq

System.Xml.Linq is a “DOM like” APIManipulates an XML tree in memory

Naturally work with both “full documents” and “fragments”The two key classes in System.Xml.Linq

Page 7: LINQ to XML

7

Creating XML

Page 8: LINQ to XML

System.Xml.Linq – More Classes

Page 9: LINQ to XML

9

A More Complete Document

Page 10: LINQ to XML

Xml Namespaces

Important to make namespace support easyVery natural syntax for expressing names

More control over this given by two additional classes

Page 11: LINQ to XML

11

Namespaces

Page 12: LINQ to XML

Loading Xml Content

Loading Xml is performed with;XElement.LoadXDocument.Load

Both support loading fromURI, XmlReader, TextReader

Page 13: LINQ to XML

Querying Xml Content

XElement has “navigation” methods

Descendants()Ancestors()etc.

These methods return;

IEnumerable<T>

Page 14: LINQ to XML

How does LINQ fit in here?

The query expression pattern in LINQ

Works with IQueryable<T> and IEnumerable<T>

from itemName in srcExprjoin itemName in srcExpr on keyExpr equals keyExpr

(into itemName)?let itemName = selExprwhere predExprorderby (keyExpr (ascending | descending)?)*select selExprgroup selExpr by keyExpr into itemName query-body

Page 15: LINQ to XML

IEnumerable<T> & IQueryable<T>

IEnumerable – query executed piece by piece

IQueryable – query executed in one go

whereselect

Capture & Execute

Execute

where select

Page 16: LINQ to XML

16

Loading & Querying XML

Page 17: LINQ to XML

Modifying XML

XML tree exposed by XElement and friends is not read-onlyModifications through methods such as;

XElement.Add(), XElement.Remove(), etc.Modified tree can be persisted via

XElement.Save(), XDocument.Save()Both supporting filename, TextWriter, XmlWriter.

Page 18: LINQ to XML

18

Modifying XML

Page 19: LINQ to XML

Possible Futures

LINQ to XML code still contains quite a lot of casts and strings

LINQ to XSD ( 0.2 Alpha )Generates strongly typed classes from XSDDerived from XElement, XDocument, etc.

Page 20: LINQ to XML

20

LINQ to XSD

Page 21: LINQ to XML

Summary

New XML APIWorks with or without LINQ

A lot nicer with LINQ Additional language support in VB 9Start using it today with Visual Studio 2008 & .NET Framework V3.5Shows up again in Silverlight 2

Page 22: LINQ to XML

© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS,

IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Page 23: LINQ to XML

Visual Basic

The age-old struggle of good versus evil

XML

Page 24: LINQ to XML

24

VB9 and XML

Page 25: LINQ to XML

Working with schema

Validation of an XML tree contained in XElement, XDocument can be done via the Validate method

Can optionally populate the tree with the Post-Schema Validation InfoSet

Allows for querying via the GetSchemaInfo methodMeans default values from the schema are now in place

Page 26: LINQ to XML

26

Schema

Page 27: LINQ to XML

Working with XPath

XPath available within the context of LINQ to XML

Remember the System.Xml.XPath namespaceExtension methods

XPathEvaluate()XPathSelectElement()XPathSelectElements()

Returned data is one or more XElements – not an XPathNavigator

Page 28: LINQ to XML

28

XPath

Page 29: LINQ to XML

Working with large XML files

DOM like API’s not usually suited to processing large XML files

Memory usage of the DOM relates to the size of the fileStreaming input files

No generic solution to this in LINQ to XMLRecommended pattern around using C# iterators to build your own axis function based on XmlReader

Streaming output filesXStreamingElement class assists in this caseDoes not build the XML tree from the query – captures it and executes it at serialisation time

Page 30: LINQ to XML

30

Streaming XML Files