public void ConvertXMLToDataStructure() { XElement xmlRoot = null; // Read data from XML and convert a Hashtable try { xmlRoot = XElement.Load(@"C:\temp\SessionData.xml"); } catch (CustomException CE) { CE.DumpException(); } // Get a sequence of trainings IEnumerable <XElement> trainings = xmlRoot.Elements(); foreach (var training in trainings) { //Temp variables before data structure push int id = int.Parse(training.Attribute("ID").Value); string trainingName = training.Element("Name").Value; int duration = int.Parse(training.Element("TrainingDuration").Value); TrainingNames.Add(id, trainingName); TrainingWithDuration.Add(id, duration); } }
public void convertRawDataToXML(String path) { try { lines = File.ReadAllLines(path); } catch (CustomException CE) { CE.DumpException(); } try { if (File.Exists(FilePath)) { File.Delete(FilePath); } fs = new System.IO.FileStream(FilePath, FileMode.CreateNew, FileAccess.ReadWrite); xmlDoc = new XmlDocument(); } catch (CustomException CE) { CE.DumpException(); } int TrainingID = 1; //Skip the first 2 headers from the input file lines = lines.Skip(2).ToArray(); foreach (string line in lines) { string[] contents = line.Split(new char[] { '|' }); int ID = TrainingID; string Name = contents[0]; string TrainingDuration = contents[1].Replace("min", ""); Training training = new Training(ID, Name, TrainingDuration); TrainingList.Add(training); TrainingID++; } try { serialization.Serialize(fs, TrainingList); fs.Close(); serialization = null; } catch (CustomException CE) { CE.DumpException(); } }
static void Main(string[] args) { Hashtable LeftOverSessions = new Hashtable(); ICollection Session2TrainingIDComplete; List <int> TrainingIDs = new List <int>(); ExtractSessionDetailsToXML DS = new ExtractSessionDetailsToXML(); TrainingOrganizer organizer = new TrainingOrganizer(); String path = null; const int Session1Slot = 120; const int Session2Slot = 120; int AvailableSession1Hours = Session1Slot; int AvailableSession2Hours = Session2Slot; //Read data from command line and convert to xml try { if (args.Any()) { path = args[0]; } DS.convertRawDataToXML(path); } catch (CustomException CE) { CE.DumpException(); } //Read from xml and convert to hashtable ds try { organizer.ConvertXMLToDataStructure(); } catch (CustomException CE) { CE.DumpException(); } // Get a random collection of the training id keys. organizer.GetRandomTrainingIDList(TrainingNames); foreach (int LocalTID in organizer.TrainingIndexes.ToArray()) { if ((int)TrainingWithDuration[LocalTID] <= AvailableSession1Hours) { AvailableSession1Hours = AvailableSession1Hours - (int)TrainingWithDuration[LocalTID]; Console.WriteLine(TrainingNames[LocalTID] + ": " + TrainingWithDuration[LocalTID] + " min"); } else { LeftOverSessions.Add(LocalTID, TrainingWithDuration[LocalTID]); } } if (AvailableSession1Hours > 0) { Console.WriteLine("Adhoc Break" + ": " + AvailableSession1Hours + " min"); } // Print Lunch Break Console.WriteLine("Lunch Break :" + "1HR"); Session2TrainingIDComplete = LeftOverSessions.Keys; foreach (int LocalTID in Session2TrainingIDComplete) { if ((int)LeftOverSessions[LocalTID] <= AvailableSession2Hours) { AvailableSession2Hours = AvailableSession2Hours - (int)LeftOverSessions[LocalTID]; Console.WriteLine(TrainingNames[LocalTID] + ": " + LeftOverSessions[LocalTID] + " min"); } } if (AvailableSession2Hours > 0) { Console.WriteLine("MISC/BREAKS" + ": " + AvailableSession2Hours); } Console.ReadKey(); }