/// <summary> /// Append product specific data to already existing the xml file /// The existing xml file contains generic product data that is common for every product in the store /// </summary> /// <param name="xElement"></param> /// <param name="columns"></param> /// <param name="retrieveProductTypeFromName"></param> /// <exception cref="ArgumentOutOfRangeException"></exception> private static void AddProductSpecificData(XContainer xElement, IReadOnlyList <string> columns, CsvProductTypes retrieveProductTypeFromName) { var specificProduct = xElement .Elements("Item") .SingleOrDefault(x => x.Element("ProductID")?.Value == columns[(int)CsvProductItems.Id]); switch (retrieveProductTypeFromName) { case CsvProductTypes.Music: specificProduct?.Add(new XElement("Artist", columns[(int)CsvMusicItems.Artist]), new XElement("PlayTime", columns[(int)CsvMusicItems.PlayTime])); break; case CsvProductTypes.Book: specificProduct?.Add(new XElement("Author", columns[(int)CsvBookItems.Author]), new XElement("ISBN", columns[(int)CsvBookItems.Isbn]), new XElement("Pages", columns[(int)CsvBookItems.Pages])); break; case CsvProductTypes.Game: specificProduct?.Add(new XElement("AgeRestriction", columns[(int)CsvGameItems.AgeRestriction])); break; default: throw new ArgumentOutOfRangeException(nameof(retrieveProductTypeFromName), retrieveProductTypeFromName, null); } }
/// <summary> /// Method that iterates over each csv-file /// </summary> /// <param name="xElement"></param> /// <param name="lines"></param> /// <param name="productType"></param> /// <returns></returns> private static void ConvertCsvToXml(XContainer xElement, IEnumerable <string> lines, CsvProductTypes productType) { // Skip the first line that contains the headers foreach (var line in lines.Skip(1)) { var columns = line.Split(','); AddGenericXmlData(xElement, columns); AddProductSpecificData(xElement, columns, productType); } }