文本序列化ChoETL
安装:
Install-Package ChoETL
网站:https://github.com/Cinchoo/ChoETL
1、CSV文件操作
//CSV文件
Id,Name
1,Tom
2,Carl
3,Mark
//CSV读取
foreach (dynamic e in new ChoCSVReader("Emp.csv").WithFirstLineHeader())
Console.WriteLine("Id: " + e.Id + " Name: " + e.Name)
//CSV写入
List<ExpandoObject> objs = new List<ExpandoObject>();
dynamic rec1 = new ExpandoObject();
rec1.Id = 1;
rec1.Name = "Mark";
objs.Add(rec1);
dynamic rec2 = new ExpandoObject();
rec2.Id = 2;
rec2.Name = "Jason";
objs.Add(rec2);
using (var parser = new ChoCSVWriter("Emp.csv"))
{
parser.Write(objs);
}
2、XML文件操作
//XML文件
<Employees>
<Employee Id='1'>
<Name>Tom</Name>
</Employee>
<Employee Id='2'>
<Name>Mark</Name>
</Employee>
</Employees>
//xml文件读取
foreach (dynamic e in new ChoXmlReader("Emp.xml"))
Console.WriteLine("Id: " + e.Id + " Name: " + e.Name);
//xml文件写入
List<ExpandoObject> objs = new List<ExpandoObject>();
dynamic rec1 = new ExpandoObject();
rec1.Id = 1;
rec1.Name = "Mark";
objs.Add(rec1);
dynamic rec2 = new ExpandoObject();
rec2.Id = 2;
rec2.Name = "Jason";
objs.Add(rec2);
using (var parser = new ChoXmlWriter("Emp.xml").WithXPath("Employees/Employee"))
{
parser.Write(objs);
}
3、Json文件操作
//JSON文件
[
{
"Id": 1,
"Name": "Mark"
},
{
"Id": 2,
"Name": "Tom"
}
]
//JSON读取
foreach (dynamic e in new ChoJSONReader("Emp.json"))
Console.WriteLine("Id: " + e.Id + " Name: " + e.Name);
//JSON写入
List<ExpandoObject> objs = new List<ExpandoObject>();
dynamic rec1 = new ExpandoObject();
rec1.Id = 1;
rec1.Name = "Mark";
objs.Add(rec1);
dynamic rec2 = new ExpandoObject();
rec2.Id = 2;
rec2.Name = "Jason";
objs.Add(rec2);
using (var parser = new ChoJSONWriter("Emp.json"))
{
parser.Write(objs);
}