Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 LINQ to XML Fagdag Fredag, 20.november 2009

2 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?

3 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

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

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

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

7 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

8 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

9 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

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

11 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

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

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

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

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

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

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

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

19 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

20 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

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

22 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

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

24 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

25 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

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

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

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


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

Similar presentations


Ads by Google