CSharp – ExpandoObject Stringified

I had a requirement to output some data as part of a number of pages in an e-commerce site. This data would change from page to page and typically get richer as a user would go through an order path in the application. The data needed to be in JSON format and was going to be used by the marketing guys to help track information about user behaviour across the order process. The data was also subject to change regularly depending on what the marketing team wanted to track. In short, I had to construct a JSON string on the server side in C#.

I had seen something like this done before where a StringBuilder was used to construct a large JSON string which was then pushed out in the response with the rest of the page markup. This approach seemed messy and difficult to maintain. I did a bit of research on some approaches to constructing a potentially complex and changing JSON object using C# and came across the ExpandoObject class. MSDN has a nice succinct explanation of ExpandoObject:

Represents an object whose members can be dynamically added and removed at run time.

You can find this class in .NET 4 in the System.Dynamic namespace. ExpandoObject lets you effectively “model” an object on the fly with properties that don’t really exist (just like using the ViewBag in ASP.NET MVC). In my case, it saved me from writing a number of classes or complex strings just to hold data that was to be output in page responses. Instead, I could use ExpandoObject and then use a JSON serializer to stringify the object.

The example below shows how easy and flexible it is to use ExpandoObject with the Newtonsoft .NET JSON library (available as a NuGet package):

dynamic orderInfo = new ExpandoObject();

orderInfo.OrderDate = DateTime.Today;

orderInfo.ProductTypesAdded = new List<dynamic>();

orderInfo.ReachedCheckout = true;
orderInfo.PaymentMethod = “Card”;

orderInfo.DeliveryInfo = new ExpandoObject();
orderInfo.DeliveryInfo.Method = “First”;
orderInfo.DeliveryInfo.Country = “UK”;

var json = JsonConvert.SerializeObject(orderInfo);

The resulting JSON is:


Empty Collections

It is better to return empty collections rather than null when writing methods. The reason being that any code calling your method then doesn’t need to explicitly handle a special null case. Returning an empty collection makes the null check redundant and results in much cleaner method calling code. In C#, the System.Linq.Enumerable class has a useful generic method called Empty. When called with a type parameter, this method returns an empty instance of IEnumerable<T> (where T is your type parameter).

An example use:

public class House
public IEnumerable<Person> CurrentOccupants { get; set; }

public House()
CurrentOccupants = Enumerable.Empty<Person>();

I also find that this method of instantiating (or returning) a collection states my intent more clearly than something like:

CurrentOccupants = new List<Person();

The MSDN documentation for Enumerable.Empty states that it caches the returned empty sequence of type TResult – so using this consistently for the same types can give (probably negligible) performance or memory usage advantages.

This pattern also reduces the chances of null reference errors from being raised in calling code. As mentioned above, the calling code then doesn’t need to go through the usual boiler-plate null check code on the returned collection. Naturally, this leads to cleaner code as the calling code can process the collection the same way if it has items or doesn’t have items within it.

CsQuery – JQuery for .NET

I’m currently working on a personal use utility application which requires some web scraping to extract data (from HTML) which can be locally processed by the application. Getting the raw HTML was straightforward enough using the HttpWebRequest and HttpWebResponse classes in the System.Net namespace.

I then reached the point where I had some raw HTML in a string that I needed to parse. After doing a quick search I found CsQuery (available as a NuGet package) which is an open source JQuery port for .NET. I was able to easily extract the data I required from the HTML using the familiar JQuery-like selectors. There is an example code snippet below which shows just how easy it is to use CsQuery.

var html = new StringBuilder();
html.Append(“<h1>Hello, world!</h1>”);
html.Append(“<p class=’intro’>This program is using CsQuery.</p>”);
html.Append(“<p id=’author’>CsQuery is a library written by James Treworgy.</p>”);
html.Append(“</body></html>”);var dom = CsQuery.CQ.Create(html.ToString());

// Get the inner text of an element by element name selector

// Get the inner text of an element by class name selector

// Get the inner text of an element by id selector

// Add a class to an element

// Update the title text using new class in selector
dom[“.title”].Text(“CSQuery – a JQuery port for .NET”);

// Now retrieve the new title by a class selector

// Pause console


CSharp – Zip Compression and Decompression .NET 4 and Earlier

Quick post about a useful third-party zip compression library if you’re working in .NET framework version 4 or earlier.

I had been working on a SQL Server Integration Services (SSIS) package. One of the steps in the package is to decompress a set of zip archives. Unfortunately, .NET script tasks in the SSIS package can only target .NET framework version 4 and earlier. This means that I couldn’t make use of the new zip compression classes introduced in .NET 4.5 (see System.IO.Compression).

Fortunately though, there are a handful of open source .NET zip libraries available. The one I opted for is called DotNetZip. DotNetZip has an intuitive API and is working well with a large number of files (I am decompressing approximately 15,000 archives). The library is available as a NuGet package. The two snippets below show just how easy it is to compress and decompress files using zip.

To compress a file into a zip archive:

using (var zipFile = new ZipFile())
// The empty string parameter ensures the file is archived to the 
// root of the archive

To decompress files out of a zip archive:

using (var zipFile = new ZipFile(@“C:\Test\Data.zip”))

CSharp How To: Create Text File And Write To It

Continuing with my new C# “how-to” theme, the code snippet below shows how to create a text file and write content to it using the StreamWriter class. Remember to add the “using System.IO;” directive at the top. Note that there is a StreamWriter constructor which supports a Boolean parameter “append” – if you pass true for this parameter and the file already exists, the StreamWriter will append content to the file rather than overwrite the existing content.

const string TextFilePath = @“C:\WriteTest.txt”;

using (StreamWriter writer = new StreamWriter(TextFilePath))
writer.WriteLine(“Hello, world”);
writer.WriteLine(“Bye, world”);