-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathProgram.cs
More file actions
141 lines (110 loc) · 4.75 KB
/
Program.cs
File metadata and controls
141 lines (110 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LINQtoCSV;
namespace SampleCode
{
class Program
{
static void Main(string[] args)
{
// NOT SHOWN IN EXAMPLE IN ARTICLE
// ReadFileWithExceptionHandling();
// ------------------
// Simple Read example
CsvContext cc = new CsvContext();
CsvFileDescription inputFileDescription = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = true,
FileCultureName = "en-US" // default is the current culture
};
IEnumerable<Product> products =
cc.Read<Product>("../../TestFiles/products.csv", inputFileDescription);
var productsByName =
from p in products
orderby p.Name
select new { p.Name, p.LaunchDate, p.Price, p.Description };
foreach (var item in productsByName) { Console.WriteLine(item); }
// ------------------
// Simple Write example 1 - without using a class with attributes
CsvFileDescription outputFileDescription = new CsvFileDescription
{
QuoteAllFields=false,
SeparatorChar = '\t', // tab delimited
FirstLineHasColumnNames = false,
FileCultureName = "nl-NL" // language/country code of The Netherlands
};
var productsNetherlands =
from p in products
where p.Country == "Netherlands"
select new { p.Name, p.LaunchDate, p.Price, p.Description };
cc.Write(
productsNetherlands,
"../../TestFiles/output-products-Netherlands.csv",
outputFileDescription);
// ------------------
// Simple Write example 2 - using a class with format attributes, etc.
// In this example, using class Product (defined in Product.cs)
List<Product> products2 = new List<Product>();
products2.Add(new Product { Name = "Desk Lamp", Country = "Netherlands", Price = 5.60M });
products2.Add(new Product { Name = "Garden Lamp", Country = "Germany", Price = 16.90M });
products2.Add(new Product { Name = "Chandelier", Country = "Austria", Price = 109.00M });
cc.Write(
products2,
"../../TestFiles/output-products-Netherlands-formatted.csv",
outputFileDescription);
}
public static void ShowErrorMessage(string errorMessage)
{
// show errorMessage to user
// .....
}
public static void ReadFileWithExceptionHandling()
{
try
{
CsvContext cc = new CsvContext();
CsvFileDescription inputFileDescription = new CsvFileDescription
{
MaximumNbrExceptions = 50 // limit number of aggregated exceptions to 50
};
IEnumerable<Product> products =
cc.Read<Product>("../../TestFiles/products.csv", inputFileDescription);
// NOT SHOWN IN EXAMPLE IN ARTICLE
foreach (var item in products) { Console.WriteLine(item); }
// Do data processing
// ...........
}
catch(AggregatedException ae)
{
// Process all exceptions generated while processing the file
List<Exception> innerExceptionsList =
(List<Exception>)ae.Data["InnerExceptionsList"];
foreach (Exception e in innerExceptionsList)
{
ShowErrorMessage(e.Message);
}
}
catch(DuplicateFieldIndexException dfie)
{
// name of the class used with the Read method - in this case "Product"
string typeName = Convert.ToString(dfie.Data["TypeName"]);
// Names of the two fields or properties that have the same FieldIndex
string fieldName = Convert.ToString(dfie.Data["FieldName"]);
string fieldName2 = Convert.ToString(dfie.Data["FieldName2"]);
// Actual FieldIndex that the two fields have in common
int commonFieldIndex = Convert.ToInt32(dfie.Data["Index"]);
// Do some processing with this information
// .........
// Inform user of error situation
ShowErrorMessage(dfie.Message);
}
catch(Exception e)
{
ShowErrorMessage(e.Message);
}
}
}
}