LINQ to XML Fagdag Fredag, 20.november 2009. In-memory XML programming interface Gamle-måten:  XmlDocument / DOM  XPath  XQuery Med LINQ:  XDocument.

Slides:



Advertisements
Similar presentations
You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advertisements

Advanced Piloting Cruise Plot.
Feichter_DPG-SYKL03_Bild-01. Feichter_DPG-SYKL03_Bild-02.
1 How to Specify Validation Information Roger L. Costello 27 December, 2008.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide.
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Document #07-12G 1 RXQ Customer Enrollment Using a Registration Agent Process Flow Diagram (Switch) Customer Supplier Customer authorizes Enrollment.
Document #07-12G 1 RXQ Customer Enrollment Using a Registration Agent Process Flow Diagram (Switch) Customer Supplier Customer authorizes Enrollment.
Document #07-2I RXQ Customer Enrollment Using a Registration Agent (RA) Process Flow Diagram (Move-In) (mod 7/25 & clean-up 8/20) Customer Supplier.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Year 6 mental test 5 second questions
Year 6 mental test 10 second questions
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
2010 fotografiert von Jürgen Roßberg © Fr 1 Sa 2 So 3 Mo 4 Di 5 Mi 6 Do 7 Fr 8 Sa 9 So 10 Mo 11 Di 12 Mi 13 Do 14 Fr 15 Sa 16 So 17 Mo 18 Di 19.
ZMQS ZMQS
Richmond House, Liverpool (1) 26 th January 2004.
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
ABC Technology Project
1 Junior Infants Letter Sounds 2 c says /c/ as in cat c.
XML and Databases Exercise Session 3 (courtesy of Ghislain Fourny/ETH)
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
VOORBLAD.
15. Oktober Oktober Oktober 2012.
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
“Start-to-End” Simulations Imaging of Single Molecules at the European XFEL Igor Zagorodnov S2E Meeting DESY 10. February 2014.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Squares and Square Root WALK. Solve each problem REVIEW:
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
We are learning how to read the 24 hour clock
1..
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Chapter 5 Test Review Sections 5-1 through 5-4.
SIMOCODE-DP Software.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
1 First EMRAS II Technical Meeting IAEA Headquarters, Vienna, 19–23 January 2009.
Addition 1’s to 20.
25 seconds left…...
Januar MDMDFSSMDMDFSSS
Week 1.
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
A SMALL TRUTH TO MAKE LIFE 100%
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
CpSc 3220 Designing a Database
Traktor- og motorlære Kapitel 1 1 Kopiering forbudt.
Introducing LINQ to XML Florin−Tudor Cristea, Microsoft Student Partner.
LINQ TO XML Mike Taulty Developer & Platform Group Microsoft UK
Presentation transcript:

LINQ to XML Fagdag Fredag, 20.november 2009

In-memory XML programming interface Gamle-måten:  XmlDocument / DOM  XPath  XQuery Med LINQ:  XDocument / XElement  LINQ query expressions Fagdag, 20.november 2009 Side 2 Hva er LINQ to XML?

Hvorfor LINQ to XML?  “Lettere” objektmodell enn DOM  Enklere syntax  Drar nytte av språkforbedringer i C#  LINQ er integrert i (C# 2008):  Intellisens  Sterkere typing  Compile-time sjekking  Bedre debugger-støtte  Functional construction (XElement,XAttribute) Fagdag, 20.november 2009 Side 3

Lese XML inn i minnet XDocument.Load("Books.xml"); XElement.Load("Books.xml"); Fagdag, 20.november 2009 Side 4

Lese XML inn i minnet XElement.Load(” ion/rss.aspx"); Fagdag, 20.november 2009 Side 5

Lese XML inn i minnet XElement.Load(XmlReader); XElement.Load(TextReader); Fagdag, 20.november 2009 Side 6

Skrive til fil XDocument xDoc = XDocument.Load("Books.xml"); xDoc.Save("Books2.xml"); XElement xElem = XElement.Load("Books.xml"); xElem.Save("Books2.xml"); Fagdag, 20.november 2009 Side 7

Skrive til stream StringBuilder output = new StringBuilder(); XmlWriterSettings xmlWriterSettings = new XmlWriterSettings {OmitXmlDeclaration = true}; using (XmlWriter writer = XmlWriter.Create(output, xmlWriterSettings)) { XElement root = myXElement; root.Save(writer); } Fagdag, 20.november 2009 Side 8

Konstruere xml XElement xml = new XElement("contacts”, new XElement("contact”, new XAttribute("contactId", "2"), new XElement(”name", ”Homer Simpson”) ), new XElement("contact”, new XAttribute("contactId", "3"), new XElement(”name", ”Mr. Burns") ) ); xml.Save(”contacts.xml”); Fagdag, 20.november 2009 Side 9

Contacts.xml Homer Simpson Mr. Burns Fagdag, 20.november 2009 Side 10

Manipulere xml: Add xElement.Add(new XElement("NewChild", "new content") ); AddFirst, AddAfterSelf, AddBeforeSelf xElement.Add(new XAttribute("NewAttribute", "attr content”) ); Fagdag, 20.november 2009 Side 11

Manipulere xml: Set Element xElement.Parse(” value "); ReplaceAll SetElementValue Value SetValue ReplaceWith( someOtherElement ) ReplaceNodes( otherElements ) Fagdag, 20.november 2009 Side 12

Manipulere xml: Set Attribute xElement.ReplaceAttributes(new XAttribute("attrName", "value")); SetAttributeValue Value SetValue Fagdag, 20.november 2009 Side 13

Manipulere xml: Remove element xElement.Remove(); xElement.RemoveNodes(); xElement.RemoveAll(); xElement.Remove(); xElement.SetValue(null); Fagdag, 20.november 2009 Side 14

Manipulere xml: Remove attribute xElement.Remove(); xElement.Attribute("someAttribute").Remove(); xElement.RemoveAttributes(); xElement.RemoveAll(); xElement.SetAttributeValue("someAttribute", null); Fagdag, 20.november 2009 Side 15

Spørringer ScottGu Blog Weblogs.ASP.NET Main Feed Bozo Blog Fagdag, 20.november 2009 Side 16

Spørringer var allElementsInXml = from element in rssXml.Descendants() select element; Fagdag, 20.november 2009 Side 17

Spørringer var feedUrls = from url in rssXml.Descendants("Url") select url; Fagdag, 20.november 2009 Side 18

Spørringer var disabledFeeds = from feed in rssXml.Descendants("Feed") where (feed.Attribute("status")!= null && feed.Attribute("status").Value.Equals("disabled")) select feed.Descendants("Url"); Fagdag, 20.november 2009 Side 19

Spørringer var feeds = from feed in rssXml.Descendants("Feed") where feed.Attribute("status") == null || feed.Attribute("status").Value != "disabled" select new FeedDefinition { Name = feed.Element("Name").Value, Url = feed.Element("Url").Value }; IList feedList = feeds.ToList(); Fagdag, 20.november 2009 Side 20

Spørringer ScottGu Blog LINQ VS2008 Fagdag, 20.november 2009 Side 21

Spørringer: sub-queries var feeds = from feed in rssXml.Descendants("Feed") select new FeedDefinition { Name = feed.Element("Name").Value, Url = feed.Element("Url").Value, Tags = (from tag in feed.Elements("Tag") orderby tag.Value select tag.Value).ToList() }; Fagdag, 20.november 2009 Side 22

Validere mot XmlSchema XmlSchemaSet schemas = new XmlSchemaSet(); schemas.Add(”MyTargetNameSpace", ”MyXmlSchema.xsd"); xmlDocument.Validate(schemas, MyValidationEventHandler); Fagdag, 20.november 2009 Side 23

Tips: Refaktorer komplekse spørringer var feeds = from feed in rssXml.Descendants("Feed“) select new FeedDefinition { Name = feed.Element("Name").Value, Url = feed.Element("Url").Value, Tags = GetTags(feed) }; private IList GetTags( XElement feed ) { var tags = from tag in feed.Elements("Tag”) orderby tag.Value select tag.Value; return tags.ToList(); } Fagdag, 20.november 2009 Side 24

Andre tips & triks  Bruk lambda-uttrykk i spørringer  Count vs Any  Avg er følsomt for lange elementlister  Cast til list for å fjerne IQueryable-context Fagdag, 20.november 2009 Side 25

Ressurser    custom-rss-feed-reader-with-it.aspx custom-rss-feed-reader-with-it.aspx  Fagdag, 20.november 2009 Side 26

Spørsmål? Fagdag, 20.november 2009 Side 27

BEKK CONSULTING AS SKUR 39, VIPPETANGEN. P.O. BOX 134 SENTRUM, 0102 OSLO.