XML to Key Value Pairs (Works well for exporting data to Excel)

On a recent project, I had to provide export functionality for a web grid’s data source that contained wildly varying data structures. The XML structure contained anywhere between 1 to 10 levels of nesting and we needed to be able to export this data into an Excel or comma separate values file.

This code will show you how to turn a complex XML tree structure into a list of key-value pairs.

For example, here is a relatively simple data structure:

In order to get the structure ready for data export, this code turns the XML into a list of key value pairs.

And the console app…

Here is the output:

output

Posted in C#          

Leave a Reply

Your email address will not be published. Required fields are marked *