Question

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.

Était-ce utile?

La solution

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);
        }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top