如何解析作为参数传递给方法的CSV [英] How to parse CSV that is passed as a parameter to a method
问题描述
我有一个包含CSV文件内容的字符串。
我需要将这些值提取到我的对象。
我没有从CSV文件获取CSV将其作为参数(数据)接收。
并在 exmple他们使用file.csv 来解析数据。
我试图加载到 MemoryStream
但没有运气。 / p>
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);
此外,我不知道如果我选择好的框架来解析CSV到自定义对象,重要的是值可以有逗号,该框架可以处理这个。
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只接受一个StreamReader,而不是流。尝试这样:
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);
}
这篇关于如何解析作为参数传递给方法的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!