C# 有没有像 CSV Serializer 这样的东西?(类似于 XmlSerializer)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11123995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there such thing as a CSV Serializer? (similar to XmlSerializer)
提问by Steve Konves
I am toying around with serializing and deserializing CSV files and I am wondering if there is an existing library, similar in concept to the XmlSerializer, which can declaratively define objects and (de)serialize them to/from a file or stream. I have looked around a bit but have not found anything focused on serialization. I already have pretty solid code for parsing CSV documents per RFC 4180, but what would be really helpful is the serialization part. What I am notlooking for is just a parser, advice to use String.Split(), etc.
我正在玩弄序列化和反序列化 CSV 文件,我想知道是否有一个现有的库,在概念上类似于 XmlSerializer,它可以声明性地定义对象并将它们(反)序列化到/从文件或流。我环顾四周,但没有发现任何专注于序列化的内容。我已经有非常可靠的代码来解析每个 RFC 4180 的 CSV 文档,但真正有用的是序列化部分。我不寻找的只是一个解析器,使用 String.Split() 的建议等。
Is there an existing project out there, or should I build one?
是否有现有项目,还是我应该建立一个?
Bonus etiquette question: if I do end up rolling my own serializer, is it appropriate to answer this question with a link to the codeplex project?
额外的礼仪问题:如果我最终滚动了自己的序列化程序,用 codeplex 项目的链接回答这个问题是否合适?
回答by Jo?o Angelo
You should take a look into FileHelpers Library.
您应该查看FileHelpers Library。
Some sample code from their site:
他们网站上的一些示例代码:
using FileHelpers;
// First declare the record class
[DelimitedRecord(",")]
public class SampleType
{
public string Field1;
public int Field2;
}
public void WriteExample()
{
FileHelperEngine engine = new FileHelperEngine(typeof(SampleType));
SampleType[] records = new SampleType[1];
records[0] = new SampleType();
records[0].Field1 = "Hello World";
records[0].Field2 = 12;
engine.WriteFile("destination.txt", records);
// Now the file contains the created record in this format:
//
// Hello World,12
}
回答by wsanville
I've used this project (CsvHelper)in the past, and it works similar to the build in .NET serializer classes in the sense that you use attributes to craft the input/output.
我过去使用过这个项目 (CsvHelper),它的工作原理类似于 .NET 序列化程序类中的构建,因为您使用属性来制作输入/输出。
There's really no need to roll your own, since there are tons out there. If you do end up rolling your own, feel free to post it. Most users, when answering a question with something they've written themselves (or are affiliated in some way) usually give a disclaimer saying so as a courtesy.
真的没有必要自己动手,因为那里有很多。如果您最终自己滚动,请随时发布。大多数用户在用他们自己写的(或以某种方式有关联的)回答问题时,通常会出于礼貌给出免责声明。
回答by Will Munn
I would highly recommend servicestack.textfor this purpose. Avialable on nuget:
为此,我强烈推荐servicestack.text。可在 nuget 上使用:
Install-package servicestack.text
It suports serialization to many data formats and unlike the built in XmlSerializer, you don't need to decorate all your properties with attributes. Here's an example to serialize to CSV.
它支持多种数据格式的序列化,与内置的 XmlSerializer 不同,您不需要用属性来装饰所有的属性。这是一个序列化为 CSV 的示例。
using ServiceStack.Text;
...
var csv = CsvSerializer.SerializeToCsv(new[]{
new Dog () {
Bark = "Woof!",
Male = true,
Size = 10
}});

