FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Web Development
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
August 07, 2008
Comparing LINQ-to-XML with XPath

(Page 1 of 4)
Paul Kimmel
Paul Kimmel is an application architect and author of more than a dozen books, including LINQ Unleashed for C#, on which this article is based. Courtesy Pearson Education, InformIT. All rights reserved.


A few years ago, I wrote a Blackjack game. The game uses statistics from a book on expert play and coaches the player in the statistically best play based on the player's and dealer's hands. (The game and source is available from my website at www.softconcepts.com.) The game was good enough that a programmer from Harrah's in Biloxi asked to use the source, and my understanding is that it is a pillow favor provided at the casino. In this article, game statistics from a round of play were saved as an XML file and that file is used for the demos. (You can download the game and save your own statistics or use the XML provided in Listing One.)

<?xml version="1.0" encoding="utf-8"?>
<Blackjack>
  <Player Name="Player 1">
    <Statistics>
       <AverageAmountLost>-28.125</AverageAmountLost>      <AverageAmountWon>30.681818181818183</AverageAmountWon>
       <Blackjacks>1</Blackjacks>
       <Losses>8</Losses>44
       <NetAverageWinLoss>5.9210526315789478</NetAverageWinLoss>
       <NetWinLoss>112.5</NetWinLoss>
       <PercentageOfBlackJacks>0.041666666666666664</PercentageOfBlackJacks>
       <PercentageOfLosses>33.333333333333329</PercentageOfLosses>
       <PercentageOfPushes>16.666666666666664</PercentageOfPushes>
       <PercentageOfWins>45.833333333333329</PercentageOfWins>
       <Pushes>4</Pushes>
       <Surrenders>1</Surrenders>
       <TotalAmountLost>-225</TotalAmountLost>
       <TotalAmountWon>337.5</TotalAmountWon>
     <Wins>11</Wins>
    </Statistics>
  </Player>
</Blackjack>
Listing One: Statistics from a Round of Play in the Blackjack Game Saved to an XML File

Examples of how to use the cards.dll are all over the web, including in some of my articles, such as Programming for Fun and Profit -- Using the Card.dll.

The basic flow of the subsections that follow is that you are shown some code that uses LINQ-to-XML to query nodes followed by an equivalent XPath query that accomplishes the same goal. (You don't need both; in practice, use one or the other.)

1 Introduction | 2 Using Namespaces | 3 Finding Children | 4 Transforming XML Data Using Functional Construction Next Page
TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Looking for a new job? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:



    MICROSITES
    FEATURED TOPIC

    ADDITIONAL TOPICS

    INFO-LINK



     




    Techweb
    Informationweek Business Technology Network
    InformationweekInformationweek 500Informationweek 500 ConferenceInformationweek AnalyticsInformationweek Events
    Informationweek MagazineGlobal CIOIWK Government ITbMightyByte and SwitchDark Reading
    Digital LibraryIntelligent EnterpriseInternet EvolutionNetwork ComputingPlug Into The CloudDr. DobbsContentinople
    space
    TechWeb Events Network
    InteropVoiceConWeb 2.0 ExpoWeb 2.0 SummitEnterprise 2.0Mobile Business ExpoNoJitter
    Black HatGTECEnergy CampCloud ConnectGov 2.0 ExpoGov 2.0 Summit
    space
    Light Reading Communications Network
    Light ReadingLight Reading AsiaUnstrungCable Digital NewsInternet EvolutionPyramid Research
    Heavy ReadingLight Reading LiveLight Reading InsiderEthrnet ExpoTelco TVTower Technology Summit
    space
    Financial Technology Network
    Advanced TradingBank Systems and TechnologyInsurance and TechnologyWall Street and TechnologyAccelerating WallstreetBST SummitBuyside Trading SummitIT Summit
    space
    Microsoft Technology Network
    MSDNTechNetTotal IT ProTotal Dev ProNET Total Dev Pro CommunitySQL Total Dev Pro Community
    space