【发布时间】:2016-06-24 09:05:36
【问题描述】:
我有以下刺痛
const string csv = "Foo1,Foo2,Foo3,Foo4,Foo5,Foo6,Ping Pong\n" +
"2016-02-29,1437.530029,1445.839966,1433.77002,1436.930054,34016300,1436.930054\n" +
"2016-02-25,1431.439941,1431.439941,1421.280029,1426.97998,78076500,1426.97998\n" +
"2016-02-24,1430.459961,1432.430054,1417.449951,1419.790039,29049900,1419.790039\n";
如何将其转换为 List<Model>
在哪里
public class Model
{
public string Foo1 { get; set; }
public string Foo2 { get; set; }
public string Foo3 { get; set; }
public string Foo4 { get; set; }
public string Foo5 { get; set; }
public string Foo6 { get; set; }
public string Ping_Pong { get; set; }
}
注意我的原始 csv 中的 Ping Pong 标头
我尝试使用CsvHelper,但没有成功,因为它采用的是流而不是字符串,我尝试转换解析失败
EDIT 是否使用CsvHelper对我来说无所谓,最后我想将csv转换为List<Model>
我该怎么做?
【问题讨论】:
-
为什么不在这里实现自定义解析逻辑?
-
或者你可以很容易地将字符串转换为流: var ms = new MemoryStream(new UTF8Encoding.GetBytes(csv));
-
有一些方法可以将
string转换为Streamstackoverflow.com/questions/1879395/… -
我建议你不要放弃 csvhelper——我已经取得了很大的成功。它可以使用 TextReader,您可以使用 StringReader 向阅读器显示您的字符串: var myReader = new StringReader(thecsvString):