public static string SerializeData(IEnumerable <object> data, CsvDataDescriptor descriptor = null, CsvParser.DelimiterType delimiter = CsvParser.DelimiterType.Comma)
        {
            List <List <string> > list = new List <List <string> >();

            if (descriptor == null)
            {
                List <Type> list2 = new List <Type>();
                foreach (object current in data)
                {
                    Type type = current.GetType();
                    if (!list2.Contains(type))
                    {
                        list2.Add(type);
                    }
                }
                descriptor = CsvDataDescriptor.CreateDescriptor(list2, null);
            }
            list.Add(descriptor.CreateCsvHeader());
            foreach (object current2 in data)
            {
                list.Add(CsvSerializer.Serialize(current2, descriptor));
            }
            return(CsvParser.ConvertToCsv(list, delimiter));
        }
        public static List <T> DeserializeData <T>(string csvString, CsvDataDescriptor descriptor = null, int headerRow = 0, CsvParser.DelimiterType delimiter = CsvParser.DelimiterType.Auto) where T : new()
        {
            List <List <string> > parsedCsv = CsvParser.ParseString(csvString, delimiter, true);

            return(CsvSerializer.DeserializeData <T>(parsedCsv, descriptor, headerRow));
        }
예제 #3
0
        public static void ParseString(string csvString, CsvParser.ParserCallback dataHandler, CsvParser.DelimiterType delimiterType = CsvParser.DelimiterType.Auto, bool ignoreEmptyLines = true)
        {
            csvString = csvString.Replace("\r", string.Empty);
            CsvParser.ParserCallback parserCallback = delegate(List <string> dataRow)
            {
                if (ignoreEmptyLines && dataRow.Count == 0)
                {
                    return;
                }
                dataHandler(dataRow);
            };
            char c;

            switch (delimiterType)
            {
            case CsvParser.DelimiterType.Auto:
                c = CsvParser.DetectDelimiter(csvString);
                break;

            case CsvParser.DelimiterType.Comma:
                c = ',';
                break;

            case CsvParser.DelimiterType.Semicolon:
                c = ';';
                break;

            case CsvParser.DelimiterType.Tab:
                c = '\t';
                break;

            default:
                c = ',';
                break;
            }
            List <string> list = new List <string>();

            char[] array = new char[]
            {
                c,
                '\n'
            };
            int i = 0;

            while (i < csvString.Length)
            {
                int    num;
                string text;
                if (csvString[i] == '"' && CsvParser.GetEndQuoteIndex(csvString, i, array, out num))
                {
                    if (num == -1)
                    {
                        Debug.LogError("Possibly corrupted CSV file, correct quote missing!");
                        text = csvString.Substring(i + 1, num - i - 1);
                    }
                    else
                    {
                        text = csvString.Substring(i + 1, num - i - 1);
                        i    = num + 1;
                        text = text.Replace("\r", string.Empty);
                        text = text.Replace("\"\"", "\"");
                    }
                }
                else
                {
                    int num2 = csvString.IndexOfAny(array, i);
                    if (num2 == -1)
                    {
                        text = csvString.Substring(i);
                        i   += text.Length;
                        text = text.Replace("\r", string.Empty);
                    }
                    else if (num2 == i)
                    {
                        text = string.Empty;
                    }
                    else
                    {
                        text = csvString.Substring(i, num2 - i);
                        i   += text.Length;
                        text = text.Replace("\r", string.Empty);
                    }
                }
                list.Add(text);
                if (i == csvString.Length)
                {
                    parserCallback(list);
                    break;
                }
                if (csvString[i] == c)
                {
                    i++;
                }
                else
                {
                    if (csvString[i] != '\n')
                    {
                        Debug.LogError("This shouldn't happen!");
                        int num3 = i - 50;
                        if (num3 < 0)
                        {
                            num3 = 0;
                        }
                        Debug.Log(csvString.Substring(num3, 100));
                        break;
                    }
                    parserCallback(list);
                    list = new List <string>();
                    if (csvString[i] == '\r')
                    {
                        i++;
                    }
                    i++;
                }
                if (i == csvString.Length)
                {
                    if (list.Count > 0)
                    {
                        parserCallback(list);
                    }
                    break;
                }
            }
        }
예제 #4
0
        public static List <List <string> > ParseFile(string fileName, CsvParser.DelimiterType delimiterType = CsvParser.DelimiterType.Auto, bool ignoreEmptyLines = true)
        {
            string csvString = File.ReadAllText(fileName);

            return(CsvParser.ParseString(csvString, delimiterType, true));
        }