Skip to content

Instantly share code, notes, and snippets.

@andy-williams
Created December 9, 2014 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andy-williams/d4984f91427d7807409d to your computer and use it in GitHub Desktop.
Save andy-williams/d4984f91427d7807409d to your computer and use it in GitHub Desktop.
Download Service, Reading XML
public class DownloadsService
{
private readonly IDictionary<string, IList<Download>> _downloadsCollections;
public DownloadsService()
{
// may be changed as a separate config service if we need to change it somehow
_downloadsCollections = GetDownloadsCollections();
}
public IEnumerable<Download> GetDownloads(string name)
{
return _downloadsCollections[name] ?? new List<Download>();
}
private IDictionary<string, IList<Download>> GetDownloadsCollections()
{
var result = new Dictionary<string, IList<Download>>();
var xmlConfig = AppDomain.CurrentDomain.BaseDirectory + "\\" + ConfigurationManager.AppSettings.Get("downloads.config");
using (var xmlStream = new StreamReader(xmlConfig))
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(xmlStream);
var collections = xmlDoc.GetElementsByTagName("collection");
foreach (var collection in collections)
{
var collectionNode = (XmlNode) collection;
var collectionName = collectionNode.Attributes["name"].Value;
var downloadItems = collectionNode.ChildNodes;
var downloadList = new List<Download>();
result.Add(collectionName, downloadList);
foreach (var downloadItem in downloadItems)
{
var downloadNode = (XmlNode) downloadItem;
var name = downloadNode.Attributes["name"].Value;
var file = downloadNode.Attributes["file"].Value;
downloadList.Add(new Download()
{
Name = name,
FileUrl = file
});
}
}
return result;
}
return new Dictionary<string, IList<Download>>();
}
}
public class Download
{
public string Name { get; set; }
public string FileUrl { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment