Xamarin: Parsing a CSV File From the Net

By June 22, 2015 News One Comment
ParseCSV2

In Friday’s post, I showed a quick and dirty method to create a Comma Separated Value (CSV) file.  Today, we place that file up in the cloud, and retrieve it, and parse it into instances of an object, and then we display those objects in a ListView.

Begin by creating a new solution, I called mine ParseCSV2.  Inside the PCL, set the ParseCSV2.cs App constructor to point to MainPage.

In MainPage we want to have a listbox that will display our people in groups, based on the first letter of their first name.  We’ve seen how to do so in this article

The tricky part today is getting and parsing the CSV file.  We begin in MainPageViewModel.cs, where we declare an instance of DataRepository and a property People  

The Model

This raises two questions: What is a DataRepository and, for these purposes, what is a Person?  Let’s start with the easier question, and look in the Model folder for the definition of a Person,

I’ve kept is very simple; in fact I’ve matched the class we used yesterday, and more important, I’ve matched the data in the CSV file.

The DataRepository is a whole lot more complex, and we’ll dive into it later.  For now, suffice to say that it is responsible for obtaining the CSV file and for parsing and grouping the returned data.

Initialize

Returning to MainPageViewModel we see an Initialize method which is called by MainPage in MainPage’s OnAppearing method,

In the ViewModel’s Initialize method we set the People property’s backing value to be an ObservableCollection of Person and we set the private member repo to be a new instance of DataRepository,

Get Data

Notice that this method ends with an asynchronous call to GetData.  GetData in this class calls repo’s GetData and returns a grouped set of people,

Clearly the DataRepository is where all the fun is.

We start by declaring a couple private ObservableCollections and the Constructor,

The key method is GetData, which returns (ready for this??) a Task of an ObservableCollection of a Grouping of a string and Person object,

Inside this method, we declare a few variables,

With these, we are ready to begin the process of retrieving the People.csv file from the cloud (where cloud, in this case, means I stashed them on my web server):

Unpacking this, we see that we use HttpWebRequest to call create passing in our Url.  With the request in hand, we can get the WebResponse object, and with that we can get a stream. This allows us to instantiate a StreamReader, which is all we need.

We now turn to the NuGet package CsvReader to parse the CSV file itself.

Grouping

CsvHelper is quite capable of reading the entire class in one line, but here we open the kimono a bit to see it obtaining each member variable in turn.   

Once we have the objects, we want to group them, and for that we use a simple Linq statement,

The end result is that we take a CSV file, which is very easy for non-technical people to create, and turn it into a nice mobile appParseCSV2

The following two tabs change content below.

Jesse Liberty

Director of Technology Development at Falafel Software
Jesse Liberty is Director of New Technology Development for Falafel Software, an author and he creates courses for Pluralsight . He is a Microsoft MVP, a Xamarin MVP and a Certified Xamarin Mobile Developer. Liberty hosts the popular Yet Another Podcast and his blog is considered required reading. He was a Senior Evangelist for Microsoft, Distinguished Software Engineer at AT&T; Software Architect for PBS and Vice President of Information Technology at Citibank.

Latest posts by Jesse Liberty (see all)