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
 Wednesday, May 16, 2007
Mike Champion has a great post that offers up some theories for why LINQ to XML has been getting positive user reviews.  In short, he thinks it could be because Anders is "the man", and can read every developers mind.  He also has a magical crystal ball that he can look into to figure out what to include and more importantly what to exclude from the LINQ to XML API.  I might not be getting all the details of Mike's post right, so you *may* want to read what he said yourself rather than taking my word for it.

I've been working with LINQ to XML as part of my work on our LINQ book, and I must say it's a seriously kick ass XML API.  I've done a lot of XML work in the past and I've never had as much fun working with XML as I do when using LINQ to XML.  The key highlights for me are:
  • Functional Construction: Have you ever created XML using existing .NET XML API's?  Enough said, functional construction beats the pants out of anything else out there for creating XML.
  • Powerful Transformation capabilities: By combining the powerful querying capabilities of LINQ with the functional construction pattern for XML creation LINQ to XML provides a lot of really nice transformation capabilities.  In addition to supporting superb XML to XML transformations, we also get killer support for transforming our XML into object structures, LINQ to SQL objects, and whatever else you can think up.
  • LINQ: What else needs to be said, sprinkling LINQ query support on top of an XML API makes for a kick ass query experience.  We get the best of XPath and XQuery but in a much nicer, cleaner, and straightforward API.
Now that I'm back on the "blogging wagon" I'll be looking to start the LINQ to XML for president campaign that I promised way back when.

Wednesday, May 16, 2007 12:14:55 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [2]  |  Trackback
 Wednesday, March 07, 2007
I'm going to document the things I run into as I transition from the May 2006 CTP of LINQ to the full March 2007 CTP of Orcas. Tonight after sorting out my data source issue I ran into a compile error with my Visual Basic code samples that are using XML Axis properties. The error is "XML Axis properties do not support late binding". Hopefully I'll be able to post back here shortly with an answer to why I'm getting this....

UPDATE:  Thanks to a comment from Avner I figured out that this is due to a regression in the capability of VB9 to infer types.  Previously things worked swimmingly with the following code:

    Dim rss = XElement.Load("rss.xml")
    Dim items = rss...<item>

With the March CTP the type of "rss" needs to be explicitly defined like so:

    Dim rss As XElement = XElement.Load("rss.xml")
    Dim items = rss...<item>

I like the old way better :)


 |  |  | 
Thursday, March 08, 2007 3:00:27 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [4]  |  Trackback
 Monday, March 05, 2007
There has been a lot of talk about LINQ as well as LINQ to SQL.  People seem to be genuinely interested in what they're going to offer.  LINQ to XML on the other hand doesn't seem to be getting much love.  I for one think it deserves it.  In the coming weeks I'm going to start a marketing campaign for LINQ to XML that will aim to convince you that LINQ to XML deserves some love as well.  Before getting started, let me ask the question, why aren't you giving LINQ to XML any love? 

Tuesday, March 06, 2007 3:25:18 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [3]  |  Trackback
 Friday, February 16, 2007
I'm with Mike, C# 3.0 needs the Visual Basic 9 XML Syntax.  Maybe "needs" is a strong word, since the functional construction model used for building XML with LINQ to XML is head and shoulders above what we have available in today's XML API's, but it sure would be nice!  Since the VB guys already have it done it should be a piece of cake to move over.  Those Microsoft guys have mad skills, I expect it to be in the next CTP! :)

Friday, February 16, 2007 1:36:33 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [9]  |  Trackback
 Sunday, February 11, 2007
Over on the XML Team's blog, Avner outlines some VB 9.0 XML changes that will be showing up in the February CTP.   As I've mentioned in the past VB 9's XML features are very compelling, so much so that they have me wanting to write code in VB which I haven't done since I started working with .NET.  The major changes in the February CTP include:

  • Attribute axis property is string
  • Global Xml namespace syntax has changed
  • Added auto-completion
  • Xml names are VB symbols
  • Improved error handling
For the full details checkout Avner's VB 9.0 Xml in Visual Studio "Orcas" February CTP post.

 |  | 
Sunday, February 11, 2007 7:24:44 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback