Created
December 9, 2014 16:32
-
-
Save andy-williams/d4984f91427d7807409d to your computer and use it in GitHub Desktop.
Download Service, Reading XML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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