Expansion on a Theme
This tip is an expanded version of what can be found here, where I showed how to save a single instance of a class (an object) to an XML file.
This tip shows how to save a collection (generic list) of objects to an XML, JSON, or CSV file.
All are easy; the only "special" thing you have to do to get the JSONification of the object collection to be written out is to add a reference to
Json.NET to your project (IOW, NuGet Newtonsoft's JSON.NET from within Visual Studio).
I am attaching the source, but in a nutshell, it defines a class, populates a generic list of that class with a few instances of the class, and then
writes that list out as XML and/or JSON and/or CSV. Again, there's not much to it, as you can see in the code snippets below.
Define a generic list and a class:
private List<BilingualBook> classicsBilingual;
public class BilingualBook
{
public string Author { get; set; }
public string SourceLang { get; set; }
public string TargetLang { get; set; }
public string EnglishTitle { get; set; }
public string PaperbackURI { get; set; }
public string KindleURI { get; set; }
public string ImageURI { get; set; }
}
Populate, or hydrate/inflate the generic list with instantiations of the class:
private void Popul8Classics()
{
classicsBilingual = new List<BilingualBook>();
var AtWi80Daze = new BilingualBook
{
Author = "Jules Verne",
SourceLang = "French",
TargetLang = "English",
EnglishTitle = "Around the World in 80 Days",
PaperbackURI = GetProductURI("1495308081"),
KindleURI = GetProductURI("B00I0DOYRE"),
ImageURI = GetImageURI("51BCZUX2-dL")
};
classicsBilingual.Add(AtWi80Daze);
var gulliver = new BilingualBook
{
Author = "Jonathan Swift",
SourceLang = "English",
TargetLang = "French",
EnglishTitle = "Gulliver's Travels",
PaperbackURI = GetProductURI("1495374688"),
KindleURI = GetProductURI("B00I5319ZO"),
ImageURI = GetImageURI("517O76OyaWL")
};
classicsBilingual.Add(gulliver);
. . .
Save the generic list as XML:
private void SaveAsXML(List<BilingualBook> classics)
{
const string xmlFilename = "FitTClassics.xml";
var writer = new System.Xml.Serialization.XmlSerializer(typeof(
List<BilingualBook>);
var file = new StreamWriter(xmlFilename);
writer.Serialize(file, classics);
file.Close();
}
And/or save the generic list as JSON:
private void SaveAsJSON(List<BilingualBook> classics)
{
const string jsonFilename = "FitTClassics.json";
string jsonRepresentation = JsonConvert.SerializeObject(classics);
File.WriteAllText(jsonFilename, jsonRepresentation);
}
And/or save the generic list as CSV:
private void SaveAsCSV(List<BilingualBook> classicsBilingual)
{
const string csvFilename = "FitTClassics.csv";
var csvFile = new List<string>();
foreach (BilingualBook classic in classicsBilingual)
{
csvFile.Add(string.Format("{0},{1},{2},{3},{4},{5},{6}",
classic.Author, classic.SourceLang, classic.TargetLang,
classic.EnglishTitle, classic.PaperbackURI, classic.KindleURI,
classic.ImageURI));
}
File.WriteAllLines(csvFilename, csvFile);
}
Now Do Your Part
If you like this tip, stand up in your cubicle, beat your chest like a "Bam-boon", and bellow out menacingly, "The hemlock shakes in the rafter, the oak in the driving keel!" If you really like this tip (or are too chicken to do that), you can instead purchase a book from one of the PaperbackURI or KindleURI links (download the code and run it, or download the XML file).