Converting a CSV file to XML using LINQ to XML and Functional Construction

In this post we aim to transform a text file into a hierarchical XML document.  As shown in Listing 12.11, the text file will contain the following book information: the ISBN, Title, Author(s), Publisher, Publication Date, and Price.

Listing 12.11    CSV of Books

0735621632,CLR via C#,Jeffrey Richter,Microsoft Press,02-22-2006,59.99
0321127420,Patterns Of Enterprise Application Architecture,Martin Fowler,Addison-Wesley Professional,11-05-2002,54.99
0321200683,Enterprise Integration Patterns,Gregor Hohpe,Addison-Wesley 04 Professional,10-10-2003,54.99
0321125215,Domain-Driven Design,Eric Evans,Addison-Wesley Professional,08-22-2003,54.99
1932394613,Ajax In Action,Dave Krane;Eric Pascarello;Darren James,Manning Publications,10-01-2005,44.95

Our goal is to parse the data in the text file and produce a hierarchy of XML as shown below:

Listing 12.12    XML Output

<?xml version="1.0" encoding="utf-8" ?>
<books>
  <book>
    <title>CLR via C#</title>
    <authors>
      <author>
        <firstName>Jeffrey</firstName>
        <lastName>Richter</lastName>
      </author>
    </authors>
    <publisher>Microsoft Press</publisher>
    <publicationDate>02-22-2006</publicationDate>
    <price>59.99</price>
    <isbn>0735621632</isbn>
  </book>
  <book>
    <title>Patterns Of Enterprise Application Architecture</title>
    <authors>
      <author>
        <firstName>Martin</firstName>
        <lastName>Fowler</lastName>
      </author>
    </authors>
    <publisher>Addison-Wesley Professional</publisher>
    <publicationDate>11-05-2002</publicationDate>
    <price>54.99</price>
    <isbn>0321127420</isbn>
  </book>
  …
</books>

The XML is constructed in a bottom up manner with functional construction, and query expressions that select the relevant data out of the individual lines of the file are intertwined to produce the desired XML.

In order to create our desired XML we’ll need to open the text file, split each line in the file into an array, and place each item in the array into the appropriate XML element.  Let’s start with opening the file and splitting it into parts.

from line in File.ReadAllLines("books.txt")
let items = line.Split(',')
// add functional construction statements for creating the XML

We leverage the static ReadAllLines method available on the File class to read each line within the text file.  Since ReadAllLines returns a string array we can safely use it in our from clause.  To split each line we make use of the Split method available on string, as well as the let clause that is available in C#.  The let clause allows us to perform the split operation once and refer to the result in subsequent expressions.  Once we have our line split apart we can wrap each item into the appropriate XML element.  

var booksXml = new XElement("books",
  from line in File.ReadAllLines("books.txt")
  let items = line.Split(',')
  select new XElement("book",
    new XElement("title", items[1]),
    new XElement("publisher", items[3]),
    new XElement("publicationDate", items[4]),
    new XElement("price", items[5]),
    new XElement("isbn", items[0])
  );

We conveniently left the authors out of the above query since they require a little extra work.  Unlike the other fields in our text file, there can be more than one author specified for a single book.  If we go back and review the sample text file, we see that the authors are delimited by a semicolon (“;”).  

    Dave Krane;Eric Pascarello;Darren James
 
As we did with the entire line, we can Split the string of authors into an array, with each author being an individual element in the array.  To be sure we get our fill of Split, we make use of it one final time to break the full author name into first and last name parts.  Finally, we place the statements for parsing out the authors into a query, and wrap the results of our many splits into the appropriate XML.


new XElement("authors",
  from authorFullName in items[2].Split(';')
  let authorNameParts = authorFullName.Split(' ')
  select new XElement("author",
    new XElement("firstName", authorNameParts[0]),
    new XElement("lastName", authorNameParts[1])
  )
)


When we add it all together we get the final solution, which can be seen in Listing 12.13.  

Listing 12.13    Final Implementation
using System;
using System.Query;
using System.Xml.XLinq;
using System.IO;

namespace LinqToXmlSamples.FlatFileToXml {
  class Program {
    static void Main(string[] args) {
      XElement xml =
        new XElement("books",
        from line in File.ReadAllLines("books.txt")
        where !line.StartsWith("#")
        let items = line.Split(',')
        select new XElement("book",
          new XElement("title", items[1]),
          new XElement("authors",
            from authorFullName in items[2].Split(';')
            let authorNameParts = authorFullName.Split(' ')
            select new XElement("author",
              new XElement("firstName", authorNameParts [0]),
              new XElement("lastName", authorNameParts [1])
            )
          ),
          new XElement("publisher", items[3]),
          new XElement("publicationDate", items[4]),
          new XElement("price", items[5]),
          new XElement("isbn", items[0])
        )
      );
      Console.WriteLine(xml);
    }
  }
}

As we’ve seen over and over again, Linq to XML allows us to mix and match data from varying data sources into functional construction statements.  The result is a very consistent programming API for developers, which makes the way XML is created from other data sources – whether they be relational, object, or a text file – consistent and predictable.

Tags: xlinq, linq to xml, linq

# re: Converting a CSV file to XML using LINQ to XML and Functional Construction

Sunday, January 07, 2007 10:34 PM by Alex James    
That is pretty cool I hadn't seen the 'let' construct before...

# re: Converting a CSV file to XML using LINQ to XML and Functional Construction

Sunday, January 07, 2007 10:39 PM by Steve    
I'm a big fan of the let clause :)

And by the way you win the award for quickest comment ever received after the publishing of a blog post. You're prize is....um....I'll have to think of what the prize is. :)

# re: Converting a CSV file to XML using LINQ to XML and Functional Construction

Monday, January 08, 2007 12:55 PM by Thom    
I'm probably the only person reading this thinking, 'so if I use CSV instead of XML, all I have to do is call split? Awesome!'

Nicely done, though. :)

# re: Converting a CSV file to XML using LINQ to XML and Functional Construction

Monday, January 08, 2007 1:40 PM by Steve    
LINQ really makes working with any type of data nice. As you've pointed out LINQ and Split is all you need to solve all your programming challenges! :)

# re: Converting a CSV file to XML using LINQ to XML and Functional Construction

Saturday, March 31, 2007 1:38 PM by www    
likable asiatiche sesso intrepido cameriera sex ridiculous fighette masturbate gnocca nuda stravagantemente mature strip montrucchio nudo caldo gola profonda immagini professoresse pompinare faint lesbiche sesso congeniale infermiera amore studentesse tgp topless in anticamera amabile segretaria azione fuoriclasse soldato spogliarello webcam donne cinesi lieve abbassare bollente cameriera merda mogli troie foto tette cubane bramare segretaria amore hentai schemales emotivo amante dildo le zie sexy collant vip studentesse maialone faint lesbiche merda nel bagno letto ragazze sex mutandine lust diavolette ubriache anziane nude amatoriali foto travestiti indescrivibile chat

# re: Converting a CSV file to XML using LINQ to XML and Functional Construction

Tuesday, May 01, 2007 4:44 AM by lesbiche-cinquantenni@qotyko86.info    
http://www.selen-perfetto.qotyko86.info belle moglie esibizioniste http://www.putane-da-scopare.qotyko86.info desiderio bionde merda http://www.filmulete-sexi-gratis.hotyko86.info riservato piccola http://www.scarpe-eccanti.hotyko86.info vecchie troie sborra http://www.mistress-leccare-piedi.eotyko86.info tansex bari http://www.shy-ragazze-amore.eotyko86.info collant amore http://www.cyber-film.lotyko86.info troie mature grasse http://www.vecchie-infermiere-troie.lotyko86.info foto piedi famosi http://www.bollente-cowgirl.wotyko86.info osare inglese gruppo http://www.rumene-provocanti.wotyko86.info sborrate su tette

# re: Converting a CSV file to XML using LINQ to XML and Functional Construction

Tuesday, May 01, 2007 4:47 AM by lesbiche-cinquantenni@qotyko86.info    
belle chiappe femmina urinate live show galleries bellezza festa minivideo sexi meloni sodi pocce amatoriali candice michelle tette crave ragazze succhi sverginate amatoriali trentenni zoccole crack erotica island cloridi rotti adolescienti nmani scopami it fighe aperte masturbate mamme lesbiche sesso suore chiavate rabbity cameriera spogliarello gradevole fighetta frode

# re: Converting a CSV file to XML using LINQ to XML and Functional Construction

Tuesday, June 26, 2007 12:57 PM by esibizionista-nuda-pregnant@hypnhcf10.info    
latine porno grasse masturbate desiderare fighette sesso cartoon collant scopate cameriere fumetti erotici piu bollente piedi vip nudissime fra le quarantenni troie gratis avventuroso asiatiche gruppo stacy ferguson vergognoso asiatiche ubriache schizzi di merda culi tondi molto bollente succhione anteprima scopate amatoriali foto fratis pisciate troie sconosciute racconti anale ????

# re: Converting a CSV file to XML using LINQ to XML and Functional Construction

Sunday, July 22, 2007 8:44 PM by http://www.ynggwe.org/    

# re: Converting a CSV file to XML using LINQ to XML and Functional Construction

Thursday, May 15, 2008 12:11 PM by troie-anziane-foto@zuzik.pl    
galleria casalinghe ragazze giovani lesbo pompini di donne vulve rotte succhia piscia racconto dietro dildo giocattoli anali racconti sui collant fichette italiane sfondi eros buono tedesco amore dottoresse zoccole

# re: Converting a CSV file to XML using LINQ to XML and Functional Construction

Monday, June 02, 2008 1:39 PM by culi-maturi-gay@ekspresilustrowany.com.pl    
rosse fighe solletico con i piedi ridiculous segretaria dildo bocchinare segretarie hunziker figa foto films pornografici gnocca nuda gratis prendilo bruna strano fitness gradevole spogliarello ludicrous segretaria fottilo incontri interraziali

Post a Comment

 
 
Prove you're not a spammer: 
4 + 4 =