/// <summary> /// Reads the data for this import from the file specified. /// </summary> /// <param name="filename">The file to read from.</param> /// <param name="errorMessage">The error message if unsuccessful.</param> /// <returns>True if read successfully, false otherwise.</returns> public override bool ReadFromFile(string filename, out string errorMessage) { _pollsProvider = new PollsProvider(); errorMessage = string.Empty; // Try to deserialize the file into the constituency results. List <OpinionPoll> opinionPolls = new List <OpinionPoll>(); try { using (StreamReader sr = new StreamReader(filename, Encoding.Default)) { CsvReader csv = new CsvReader(sr, CultureInfo.CurrentCulture); bool foundStartDataTag = false; while (csv.Read()) { // Get the first field. string pollingCompany = csv.GetField <string>(0); // Ignore empty entries. if (string.IsNullOrWhiteSpace(pollingCompany)) { continue; } // If haven't found the header line see if this is it if (!foundStartDataTag) { if (pollingCompany.Contains(DataStartTag)) { foundStartDataTag = true; } continue; } // If here on a line with data try to convert it into a record. OpinionPoll poll = GetOpinionPollFromRecord(csv, pollingCompany); if (poll != null) { opinionPolls.Add(poll); } } } } catch (Exception e) { errorMessage = e.ToString(); return(false); } if (opinionPolls.Any()) { _pollsProvider = new PollsProvider(opinionPolls); } return(true); }
/// <summary> /// Loads the polls from a file. /// </summary> /// <param name="resultsDir">The full path of the the results files.</param> /// <param name="fileName">The full path of the notes notes file, or null if the default is to be read.</param> public PollingFileParser(string resultsDir, string fileName = null) { _pollsProvider = new PollsProvider(); Parse(resultsDir, fileName); }