문제

I have a string that contains content of CSV file.
I need to extract those values to my objects.
I don't get CSV from CSV file I receive it as a parameter (data).
And in exmple they use file.csv to parse data.
I have tried to load it in MemoryStream but without luck.

public string methos(string data)
        {
            CsvFileDescription inputFileDescription = new CsvFileDescription
            {
                SeparatorChar = ',',
                FirstLineHasColumnNames = true
            };

            CsvContext cc = new CsvContext();

            MemoryStream mStream = new MemoryStream(System.Text.ASCIIEncoding.Default.GetBytes(data));

            IEnumerable<Data datas=
            cc.Read<Data>(mStream, inputFileDescription);

Also I don't know if I pick good framework to parse CSV to custom objects but to me it's important that values can have commas and that framework can handle that.

도움이 되었습니까?

해결책

LINQtoCSV only takes a StreamReader, not a stream. Try this:

        using (MemoryStream mStream = new MemoryStream(System.Text.ASCIIEncoding.Default.GetBytes(data)))
        using (StreamReader reader = new StreamReader(mStream))
        {
            IEnumerable<Data> datas = cc.Read<Data>(reader, inputFileDescription);
        }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top