private void OpenNewContent(ConfigFile file) { if (m_configContentDic.ContainsKey(file)) { m_curConfigContent = m_configContentDic[file]; } else { ConfigContent content = null; if (file.fileType == FileType.Csv) { content = new CsvContent(file); } else if (file.fileType == FileType.Json) { content = new JsonContent(file); } else if (file.fileType == FileType.Xml) { content = new XmlContent(file); } else { return; } m_configContentDic.Add(file, content); m_curConfigContent = content; } }
/// <summary> /// Convert the current CSV to a csv object /// allows to set false the second parameter to indicate that there is no headers /// </summary> /// <param name="path"></param> /// <returns></returns> private CsvContent ParseCsvData(string path) { var csv = new CsvContent(); var parser = new ParseCsv(Configuration); var parsedFile = parser.FromFile(path); var enumerable = parsedFile as IList <string>[] ?? parsedFile.ToArray(); if (Configuration.GetContainsHeaders()) { var headrs = enumerable.Take(1); var counter = 0; foreach (var headlist in headrs) { foreach (var element in headlist) { var head = new CsvField(); head.Field = element == null ? "" : element.ToString(); head.Position = counter; csv.Headers.Add(head); counter++; } } } //skip the first row if contains headers var rows = enumerable.Skip((Configuration.GetContainsHeaders() ? 1 : 0)); foreach (var row in rows) { var counter = 0; var rowline = new CsvRow(); foreach (string element in row) { rowline.FieldValue.Add(new CsvField { Value = element == null ? "" : element.ToString(), Position = counter, Field = (csv.Headers.Any() ? csv.Headers.ElementAt(counter).Field:"") }); counter++; } csv.Rows.Add(rowline); } return(csv); }
public void Append(CsvContent other) { this.Rows.AddRange(other.Rows); }
public static CsvContent Parse(string strCSV, bool contentOnly = false, char SEPERATOR = ',') { try { if (string.IsNullOrEmpty(strCSV)) { return(null); } var rows = strCSV.Replace("\r", string.Empty).Split('\n'); if (rows.Length == 0) { return(null); } var csvContent = new CsvContent(); // special case, seperator as part of a content (which means that the content is wrapped with quotes "") // replace it to a temporary placeholder to prevent split issues and later return it back var patternPH = string.Format("(?<= \" [^\"]+? ) \\{0} (?= [^\"]+? \" )", SEPERATOR); var tempPH = "#PH#"; // handle double quotes /// var patternDQ = "\"{2}"; var tempDQ = "#DQ#"; for (var i = 0; i < rows.Length; i++) { var row = rows[i]; var hasDQ = row.Contains("\"\""); // temporary remove DQ to prevent it from damaging the PH matches if (hasDQ) { row = row.Replace("\"\"", tempDQ); } // use temporary PH to prevent the content to be splitted incorrectly row = Regex.Replace(row, patternPH, tempPH, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); if (hasDQ) { row = row.Replace(tempDQ, "\"\""); } rows[i] = row; } CsvRow csvHeaderRow = null; if (!contentOnly) { // header csvHeaderRow = new CsvRow { IsHeader = true, }; foreach (var col in rows.First().Split(SEPERATOR)) { csvHeaderRow.Columns.Add(col.Trim().Replace(tempPH, SEPERATOR.ToString())); } csvContent.Rows.Add(csvHeaderRow); } // body foreach (var row in rows.Skip(1)) { var csvRow = new CsvRow(); var cols = row.Split(SEPERATOR); // empty rows if (cols.Length == 0 || (cols.Length == 1 && string.IsNullOrWhiteSpace(cols[0]))) { continue; } for (int i = 0; i < cols.Length; i++) { csvRow.Columns.Add(new CsvColumn { Name = csvHeaderRow == null ? "" : csvHeaderRow.Columns[i].Value, // set header name as the name of the column - for indexer row["Email"] etc. Value = cols[i].Trim().Replace(tempPH, SEPERATOR.ToString()) }); } csvContent.Rows.Add(csvRow); } return(csvContent); } catch { return(null); } }