13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

LINQ to XML343need the str<strong>in</strong>g form, and the code becomes hard to format sensibly with<strong>in</strong> the strictlimits of the pr<strong>in</strong>ted page.List<strong>in</strong>g 12.17 isn’t do<strong>in</strong>g anyth<strong>in</strong>g particularly impressive, of course. In particular,it would be easy to achieve a similar effect with a s<strong>in</strong>gle XPath expression. Jo<strong>in</strong>s, however,are harder to express <strong>in</strong> XPath. They work, but they’re somewhat messy. WithLINQ to XML, we can use our familiar query expression syntax. List<strong>in</strong>g 12.18 demonstratesthis, show<strong>in</strong>g each open defect’s ID with its assignee and project.List<strong>in</strong>g 12.18Two jo<strong>in</strong>s and a filter with<strong>in</strong> a LINQ to XML queryXElement root = XmlSampleData.GetElement();var query = from defect <strong>in</strong> root.Descendants("defect")jo<strong>in</strong> user <strong>in</strong> root.Descendants("user")on (<strong>in</strong>t?)defect.Attribute("assigned-to") equals(<strong>in</strong>t)user.Attribute("id")jo<strong>in</strong> project <strong>in</strong> root.Descendants("project")on (<strong>in</strong>t)defect.Attribute("project") equals(<strong>in</strong>t)project.Attribute("id")where (str<strong>in</strong>g)defect.Attribute("status") != "Closed"select new { ID=(<strong>in</strong>t)defect.Attribute("id"),Project=(str<strong>in</strong>g)project.Attribute("name"),Assignee=(str<strong>in</strong>g)user.Attribute("name") };foreach (var defect <strong>in</strong> query){Console.WriteL<strong>in</strong>e ("{0}: {1}/{2}",defect.ID,defect.Project,defect.Assignee);}I’m not go<strong>in</strong>g to pretend that list<strong>in</strong>g 12.18 is particularly pleasant. It has lots of str<strong>in</strong>gliterals (which could easily be turned <strong>in</strong>to constants) and it’s generally pretty wordy. Onthe other hand, it’s do<strong>in</strong>g quite a lot of work, <strong>in</strong>clud<strong>in</strong>g cop<strong>in</strong>g with the possibility of adefect not be<strong>in</strong>g assigned to a user (the <strong>in</strong>t? conversion <strong>in</strong> the jo<strong>in</strong> of defect toassignee). Consider how horrible the correspond<strong>in</strong>g XPath expression would have tobe, or how much manual code you’d have to write to perform the same query <strong>in</strong> directcode. The other standard query operators are available, too: once you’ve got a querysource, LINQ to XML itself takes a back seat and lets LINQ to Objects do most of thework. We’ll stop there, however—you may have seen enough query expressions to makeyou dizzy by now, and if you want to experiment further it’s easy enough to do so.12.4.4 LINQ to XML summaryLike the other topics <strong>in</strong> this chapter, we’ve barely scratched the surface of the LINQ toXML API. I haven’t touched the <strong>in</strong>tegration with the previous technologies such as DOMand XPath, nor have I given details of the other node types—not even XDocument!Even if I were to go through all of the features, that wouldn’t come close toexpla<strong>in</strong><strong>in</strong>g all the possible uses of it. Practically everywhere you currently deal withXML, I expect LINQ to XML will make your life easier. To reiterate a cliché, the onlylimit is your imag<strong>in</strong>ation.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!