Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Web Development

Comparing LINQ-to-XML with XPath


Transforming XML Data Using Functional Construction

Functional construction is quite literally a way to create an XML tree in a single statement by chaining function calls together where subordinate calls are arguments to the calling method. (This same nesting of method calls is also often used in the CodeDOM namespace to create code graphs.)

The XML document in Listing One was created with functional construction. Quite straightforward really, the XML tree was created by chaining XElement (and XAttribute) objects together to form the shape of the XML document and calling the XElement.Save method.

private void SerializeGameStatistics(BlackJack game)
  {
    try
     {
       Statistics stats = game.Players[0].Statistics;
       //serialize game to XML
       XElement xml =
         new XElement("Blackjack",
          new XElement("Player",
            new XAttribute("Name", game.Players[0].Name),
            new XElement("Statistics",
            new XElement("AverageAmountLost", stats.AverageAmountLost),
            new XElement("AverageAmountWon", stats.AverageAmountWon),
            new XElement("Blackjacks", stats.BlackJacks),
            new XElement("Losses", stats.Losses),
            new XElement("NetAverageWinLoss", stats.NetAverageWinLoss),
            new XElement("NewWinLoss", stats.NetWinLoss),
            new XElement("PercentageOfBlackJacks",
               stats.PercentageOfBlackJacks),
            new XElement("PercentageOfLosses", stats.PercentageOfLosses),
            new XElement("PercentageOfPushes", stats.PercentageOfPushes),
            new XElement("PercentageOfWins", stats.PercentageOfWins),
            new XElement("Pushes", stats.Pushes),
            new XElement("Surrenders", stats.Surrenders),
            new XElement("TotalAmountLost", stats.TotalAmountLost),
            new XElement("TotalAmountWon", stats.TotalAmountWon),
            new XElement("Wins", stats.Wins))));
        xml.Save(
         Path.GetDirectoryName(
           Application.ExecutablePath) + "\\CurrentStats.xml");
     }
   catch{}
}
Listing Seven: Using Functional Construction, or Chains of System.Xml.Linq Objects to Shape the Desired Form of the XML Output

The method in Listing Seven was added to the Blackjack sample application. The data is derived from objects in that game, but the orchestration of the XElement objects could really be applied to any objects.

Summary

XPath is an open standard for querying and transforming XML documents; however, it is a completely different technology than C# programming, which you already know. With LINQ-to-XML, you can now do some of the things provided by XPath in a way you already know how to do them.


Rough Cuts is a Safari Books Online interactive publishing service that provides you with first access to pre-published manuscripts on hot technology topics-enabling you to stay on the cutting-edge and remain competitive. This version may not be final, the published book will be available in August 2008.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.