Monday, January 21, 2008
After many, many, many long months of work, LINQ in Action is finally done!  Fabrice, Jim, and I are very proud of the final product and really hope you enjoy it.  We've already heard a lot of positive feedback from those who purchased the Early Access Preview from Manning, and are hopefull that LINQ in Action will be a valuable resource for everyone who is trying to add LINQ to their development toolbox.  My favorite quote thus far is from Ben Hayat on the LINQ in Action forums where he said "I had gotten other books on Linq, and this book is simply the BEST!".  Now for those of you who don't know Ben, it should be very clear that he's extremely smart and intelligent and you should believe everything he says, especially when it comes to what the best LINQ book is! :D 

Since the book is on the printers as we speak, it isn't yet available on Amazon for immediate shipping, however, I've been told it should make it's way over there in the next couple of weeks.  Given that, now is a great time to head over and pre-order it!  If you want the book sooner rather than later the best way to get it is directly via Manning's website.

To keep updated on the status of the book, including errata, code samples, or to ask Fabrice, Jim, or I any questions about our LINQ book you should drop by the LINQ in Action website or the author forums on the Manning website.

Monday, January 21, 2008 2:32:52 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [3]  |  Trackback
 Friday, August 31, 2007
Scott Hanselman and Carl Franklin talk about LINQ to XML in the latest episode of Hanselminutes.  Scott's done a ton of work with existing .NET XML Api's, and has starting digging into what's available with LINQ to XML.

Friday, August 31, 2007 12:10:19 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Thursday, August 30, 2007
A while back I posted an example of how to convert a comma seperated file (CSV) to XML using LINQ to XML and functional construction.  We're in the final push to get LINQ in Action to production and as such I've been spending a lot of time going back through the chapters cleaning things up, as well as making sure both a C# and VB.NET example is provided for every code sample presented in the book.  Tonight I was converting a code sample from Chapter 12 that shows how to convert a CSV file to XML using LINQ to XML.  While the C# code is very nice, I like the VB version that is shown below even better.

Imports System.IO
Imports System.Xml.Linq

Module FlatFileToXmlWithXmlLiterals
  Sub Main()
    Dim xml As XElement = <books>
                          <%= From line In File.ReadAllLines("books.txt") _
                          Where Not line.StartsWith("#") _
                          Let items = line.Split(",") _
                          Select _
                          <book>
                            <title><%= items(1) %></title>
                            <authors>
                              <%= From authorFullName In items(2).Split(";") _
                                Let authorNameParts = authorFullName.Split(" ") _
                                Select <author>
                                         <firstName><%= authorNameParts(0) %></firstName>
                                         <lastName><%= authorNameParts(1) %></lastName>
                                       </author> _
                              %>
                            </authors>
                            <publisher><%= items(3) %></publisher>
                            <publicationDate><%= items(4) %></publicationDate>
                            <price><%= items(5) %></price>
                            <isbn><%= items(0) %></isbn>
                          </book> _
                        %>
                      </books>

    Console.WriteLine(xml)
  End Sub
End Module

As an aside, if you've been meaning to learn about LINQ, or if you've already begun your journey, now is a great time to checkout the early access edition of LINQ in Action.  We have every chapter available for download, and will very shortly be making the source code available.  We still have a little ways to go, but we're getting close!

Download the sample project here: Chapter12.FlatFileToXml.Vb.zip (10.09 KB)
Friday, August 31, 2007 1:26:49 AM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Thursday, August 02, 2007
While working on our LINQ book I've come to really enjoy LINQ to XML. I've worked with a lot of XML API's over the years, however, the LINQ to XML API is my favorite...by far. In addition to providing all the nice query facilities made available by LINQ, it also provides a lot of other great features that many people overlook. As I promised long ago, I'm going to begin to talk about the things that I enjoy about LINQ to XML in hopes that it will help you realize that the red headed step child of LINQ has some things to offer the world as well. :)

One of the most common things that we need to do when dealing with XML is transform it. We're usually transforming it into an alternate XML format, or transforming the XML into a set of objects. In this post I'm going to quickly look at some of the transformation capabilities offered by LINQ to XML. To help us get started I'm going to use the following XML which is the XML representation of a contact in Highrise.

<?xml version="1.0" encoding="UTF-8"?>
<person>
  <author-id type="integer">1436</author-id>
  <background/>
  <company-id type="integer">1226900</company-id>
  <created-at type="datetime">2007-06-09T03:13:15Z</created-at>
  <first-name>Steve</first-name>
  <group-id type="integer"/>
  <id type="integer">1226899</id>
  <last-name>Eichert</last-name>
  <owner-id type="integer"/>
  <title/>
  <updated-at type="datetime">2007-06-09T03:15:16Z</updated-at>
  <visible-to>Everyone</visible-to>
  <contact-data>
    <email-addresses type="array">
      <email-address>
        <address>steve.eichert at google mail dot com</address>
        <id type="integer">559722</id>
        <location>Work</location>
      </email-address>
    </email-addresses>
    <web-addresses type="array">
      <web-address>
        <id type="integer">942962</id>
        <location>Work</location>
        <url>http://iqueryable.com/</url>
      </web-address>
    </web-addresses>
  </contact-data>
</person>

Rather than be stuck with our contact in XML, let's see what we can do to transform the above XML into the hCard microformat. We're going to ignore a bunch of data, such as all the ids, since it doesn't have any meaning outside of Highrise. When we're done we'll end up with the much simplified XML shown below:

<div class="vcard">
  <div class="fn">Steve Eichert</div>
  <div>Email: <span class="email">steve.eichert at google mail dot com</span></div>
  <a class="url" href="http://iqueryable.com/">http://iqueryable.com/</a>
</div>

The first step for transforming our Highrise XML into the hCard microformat is to load the Highrise XML into an XElement.

XElement highriseRoot = XElement.Load("highrise-contact.xml");

We use the static Load method of XElement to load the XML contained within the "highrise-contact.xml" file that we've saved locally. I don't believe the Highrise API is officially supported at the moment so I'm not going to load the contact details directly from the highrisehq.com site. Perhaps, in a future post we can explore that as an option.

Anywho, once our XML is loaded into an XElement, we can transform our Highrise XML into the hCard microformat by building a new XElement. We'll use the Element query axis method to retrieve the first and last name of the contact, and we'll embed query expressions and make use of the Descendants query axis method for selecting all the email and web addresses for the contact within the source XML. When we put it all together we end up with the C# code below:

XElement highriseRoot = XElement.Load("highrise-contact.xml");

XElement hCard =
    new XElement("div",
        new XAttribute("class", "vcard"),
        new XElement("div",
            new XAttribute("class", "fn"),
            highriseRoot.Element("first-name") + " " + highriseRoot.Element("last-name")
        ),
        from emailElement in highriseRoot.Descendants("email-address")
        select new XElement("div",
            "Email:",
            new XElement("span",
                new XAttribute("class", "email"),
                (string) emailElement.Element("address")
            )
        ),
        from webElement in highriseRoot.Descendants("web-address")
        select
        new XElement("a",
            new XAttribute("class", "url"),
            new XAttribute("href", (string) webElement.Element("url")),
            (string) webElement.Element("url")
        )
    );

Console.WriteLine(hCard);

At first glance, the above code might be overwhelming. However, once you come to understand the power of functional construction you'll quickly realize how wonderful LINQ to XML can be for transforming XML to alternate XML formats. In addition to making it easy to transform XML into alternate XML formats, LINQ to XML also makes it very easy to transform XML into objects. If we have a Contact class defined as:

public class Contact {
    public string Name { get; set; }
    public IEnumerable<string> EmailAddresses { get; set; }
    public IEnumerable<string> Urls { get; set; }
}

We can transform the contact details in our XML into a Contact instance with the following code:

Contact contact = new Contact {
    Name = (string) highriseRoot.Element("first-name") + " " + (string) highriseRoot.Element("last-name"),
    EmailAddresses = highriseRoot.Descendants("email-address").Select(e => (string)e.Element("address")),
    Urls = highriseRoot.Descendants("web-address").Select(e => (string)e.Element("url"))
};

After looking back at the sample here I wish I had chosen an XML fragment with a little more hierarchy, however it's much too late for that now. Hopefully, the code included in this post gives you a small taste of the types of XML transformations possible with LINQ to XML. As you begin to work with LINQ to XML, you'll find that functional construction, combined with query axis methods, and query expressions provide a tremendous amount of flexibility for transforming XML. Additionally, the new object initializer syntax and LINQ to XML's ability to easily construct objects from XML makes it very easy to create objects from XML. I've attached a zip file with the code above to this post. (VS2008 Beta 2 Required) LINQtoXMLTransformSample.zip (23.69 KB)

Friday, August 03, 2007 3:20:02 AM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Monday, March 05, 2007
I had a jolly good fun time tonight updating my code samples to the March CTP of Orcas.  Well, actually, I'm not quite finished as of yet because of the wonderful error in the title of this post.  It appears VS.NET Orcas doesn't want to let me recreate my data connection for my database that I'm using for some LINQ to SQL examples. Woot!  Hopefully I'll figure out what the dealy is shortly because I've got chapters I need to finish. :)

Tuesday, March 06, 2007 3:20:47 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [1]  |  Trackback
 Tuesday, February 13, 2007
Over the last several months I've grown to have an amazing amount of respect for people who write technical books.  It's an amazingly time consuming process that involves a lot of staring at the screen wondering what the heck to write next.  There are an infinite number of ways to phrase every sentence, and your choice has a huge impact on whether or not the reader will get what you're trying to say.  On top of that, for me, writing is nowhere near as interesting as actually using the technology you're writing about.  I can't tell you how many times I've wished I could get the heck out of Word and into Visual Studio.  Layer on top a busy work and family life and you have something that I've found amazingly difficult.  Since I have a full time gig I have to write in the evenings and weekends.  With a wife and two young kids I'm not exactly oozing with tons of free time.  Writing is amazingly difficult. 

Rather than end this post and leave it as a paragraph of me complaining about how hard I'm finding writing, let me leave you with some tips that I've found to help in the writing process.
  • Find someplace quite to work.  Unlike programming (and other tasks) where a little bit of background noise and/or music can be beneficial, writing needs quite and needs thought.  Find someplace you can think.  Don't try and get a little bit of writing done while you try and do something else (such as watch after the kiddos).
  • Start with an outline of what your going to write.  Include the key goals that you have for each section and make sure you address each goal before you finish the section, or chapter.
  • With your first draft don't worry about how terrible things sounds, how rough around the edges your points and/or description are, and how utterly crappy things flow.  Get your thoughts on paper.  Once you get everything out, go back and revise mercilessly until things sounds the way you want.
  • Always think about the reader.  How will they expect to consume the information you're presenting?  What will they arleady know? What might they need to be reminded of?  How can you leverage what they know to help them learn what you're writing about?
  • Don't be boring.  This might sounds obvious but I've found it very hard to present everything you want the reader to learn without having things turn into an unending flow of technical details, which results in boredom.  It's important to remember the reader needs breaks, encouragement, and direction.  And they also need to not be bored.
  • Instead of writing blog posts about how hard writing is and then spewing off a bunch of writing tips (as if you have any clue what you're talking about) stay focused on your book and the deadlines you have....doh! :)
Wednesday, February 14, 2007 3:23:17 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [1]  |  Trackback