Write quotes round all fields except first (heading) row
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
CsvHelper has this very handy flag to set ""
round all fields (a common requirement in CSV files):
csv.Configuration.QuoteAllFields = true;
However, this also places "
round the first row, which contains field names.
How can ""
be put round all fields except the first row containing field names?
I can find nothing in Google
c# csvhelper
add a comment |
CsvHelper has this very handy flag to set ""
round all fields (a common requirement in CSV files):
csv.Configuration.QuoteAllFields = true;
However, this also places "
round the first row, which contains field names.
How can ""
be put round all fields except the first row containing field names?
I can find nothing in Google
c# csvhelper
Why do you want to?
– Caius Jard
Nov 24 '18 at 17:34
Client requirement
– niico
Nov 24 '18 at 17:40
Show the entire code you use to write the file
– Caius Jard
Nov 24 '18 at 20:24
add a comment |
CsvHelper has this very handy flag to set ""
round all fields (a common requirement in CSV files):
csv.Configuration.QuoteAllFields = true;
However, this also places "
round the first row, which contains field names.
How can ""
be put round all fields except the first row containing field names?
I can find nothing in Google
c# csvhelper
CsvHelper has this very handy flag to set ""
round all fields (a common requirement in CSV files):
csv.Configuration.QuoteAllFields = true;
However, this also places "
round the first row, which contains field names.
How can ""
be put round all fields except the first row containing field names?
I can find nothing in Google
c# csvhelper
c# csvhelper
edited Nov 24 '18 at 18:44
Nkosi
121k17143207
121k17143207
asked Nov 24 '18 at 17:32
niiconiico
3,934105397
3,934105397
Why do you want to?
– Caius Jard
Nov 24 '18 at 17:34
Client requirement
– niico
Nov 24 '18 at 17:40
Show the entire code you use to write the file
– Caius Jard
Nov 24 '18 at 20:24
add a comment |
Why do you want to?
– Caius Jard
Nov 24 '18 at 17:34
Client requirement
– niico
Nov 24 '18 at 17:40
Show the entire code you use to write the file
– Caius Jard
Nov 24 '18 at 20:24
Why do you want to?
– Caius Jard
Nov 24 '18 at 17:34
Why do you want to?
– Caius Jard
Nov 24 '18 at 17:34
Client requirement
– niico
Nov 24 '18 at 17:40
Client requirement
– niico
Nov 24 '18 at 17:40
Show the entire code you use to write the file
– Caius Jard
Nov 24 '18 at 20:24
Show the entire code you use to write the file
– Caius Jard
Nov 24 '18 at 20:24
add a comment |
2 Answers
2
active
oldest
votes
Reading the code of csvhelper it appears that the WriteHeader methods use WriteField internally, and set flags indicating that the header has been written. WriteField obeys the currently configured settings as to whether it should quote a field or not, and it bakes the quotes into the field data passed at the time it is called even though writing to the file doesn't necessarily occur at that time
As such I recommend you do something like this:
var cw = new CsvWriter(yourTextWriterOrWhatever);
cw.Configuration.QuoteNoFields = true;
cw.WriteHeader<YourClassNameHere>();
cw.NextRecord(); // without this first row is on same line as header
cw.Configuration.QuoteAllFields = true; //or set QuoteNoFields = false
cw.WriteRecords(yourCollectionOfYourClass);
Just to be clear; you cannot paste and go on this code above, you have to edit it to be valid to your context - anywhere you encounter he word "Your" needs adjusting.
The important part I'm aiming to outline is that you should
- turn quotes off in the config,
- write the headers,
- turn quoting on (either by quoting everything or disabling the quotenofields and letting csvhelper work out whether to quote or not)
- write the data records
QuoteAllFields appears to have stopped working in version 2? It's no longer available.
– niico
Dec 20 '18 at 19:09
1
Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?
– Caius Jard
Dec 20 '18 at 19:28
1
A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…
– Caius Jard
Dec 20 '18 at 19:32
This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.
– niico
Jan 20 at 16:49
add a comment |
You could use something like this
cw.Configuration.ShouldQuote = (field, context) => context.HasHeaderBeenWritten;
or
cw.Configuration.ShouldQuote = (field, context) => context.Row > 1;
But you'll need to test and adjust as needed.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460714%2fwrite-quotes-round-all-fields-except-first-heading-row%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Reading the code of csvhelper it appears that the WriteHeader methods use WriteField internally, and set flags indicating that the header has been written. WriteField obeys the currently configured settings as to whether it should quote a field or not, and it bakes the quotes into the field data passed at the time it is called even though writing to the file doesn't necessarily occur at that time
As such I recommend you do something like this:
var cw = new CsvWriter(yourTextWriterOrWhatever);
cw.Configuration.QuoteNoFields = true;
cw.WriteHeader<YourClassNameHere>();
cw.NextRecord(); // without this first row is on same line as header
cw.Configuration.QuoteAllFields = true; //or set QuoteNoFields = false
cw.WriteRecords(yourCollectionOfYourClass);
Just to be clear; you cannot paste and go on this code above, you have to edit it to be valid to your context - anywhere you encounter he word "Your" needs adjusting.
The important part I'm aiming to outline is that you should
- turn quotes off in the config,
- write the headers,
- turn quoting on (either by quoting everything or disabling the quotenofields and letting csvhelper work out whether to quote or not)
- write the data records
QuoteAllFields appears to have stopped working in version 2? It's no longer available.
– niico
Dec 20 '18 at 19:09
1
Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?
– Caius Jard
Dec 20 '18 at 19:28
1
A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…
– Caius Jard
Dec 20 '18 at 19:32
This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.
– niico
Jan 20 at 16:49
add a comment |
Reading the code of csvhelper it appears that the WriteHeader methods use WriteField internally, and set flags indicating that the header has been written. WriteField obeys the currently configured settings as to whether it should quote a field or not, and it bakes the quotes into the field data passed at the time it is called even though writing to the file doesn't necessarily occur at that time
As such I recommend you do something like this:
var cw = new CsvWriter(yourTextWriterOrWhatever);
cw.Configuration.QuoteNoFields = true;
cw.WriteHeader<YourClassNameHere>();
cw.NextRecord(); // without this first row is on same line as header
cw.Configuration.QuoteAllFields = true; //or set QuoteNoFields = false
cw.WriteRecords(yourCollectionOfYourClass);
Just to be clear; you cannot paste and go on this code above, you have to edit it to be valid to your context - anywhere you encounter he word "Your" needs adjusting.
The important part I'm aiming to outline is that you should
- turn quotes off in the config,
- write the headers,
- turn quoting on (either by quoting everything or disabling the quotenofields and letting csvhelper work out whether to quote or not)
- write the data records
QuoteAllFields appears to have stopped working in version 2? It's no longer available.
– niico
Dec 20 '18 at 19:09
1
Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?
– Caius Jard
Dec 20 '18 at 19:28
1
A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…
– Caius Jard
Dec 20 '18 at 19:32
This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.
– niico
Jan 20 at 16:49
add a comment |
Reading the code of csvhelper it appears that the WriteHeader methods use WriteField internally, and set flags indicating that the header has been written. WriteField obeys the currently configured settings as to whether it should quote a field or not, and it bakes the quotes into the field data passed at the time it is called even though writing to the file doesn't necessarily occur at that time
As such I recommend you do something like this:
var cw = new CsvWriter(yourTextWriterOrWhatever);
cw.Configuration.QuoteNoFields = true;
cw.WriteHeader<YourClassNameHere>();
cw.NextRecord(); // without this first row is on same line as header
cw.Configuration.QuoteAllFields = true; //or set QuoteNoFields = false
cw.WriteRecords(yourCollectionOfYourClass);
Just to be clear; you cannot paste and go on this code above, you have to edit it to be valid to your context - anywhere you encounter he word "Your" needs adjusting.
The important part I'm aiming to outline is that you should
- turn quotes off in the config,
- write the headers,
- turn quoting on (either by quoting everything or disabling the quotenofields and letting csvhelper work out whether to quote or not)
- write the data records
Reading the code of csvhelper it appears that the WriteHeader methods use WriteField internally, and set flags indicating that the header has been written. WriteField obeys the currently configured settings as to whether it should quote a field or not, and it bakes the quotes into the field data passed at the time it is called even though writing to the file doesn't necessarily occur at that time
As such I recommend you do something like this:
var cw = new CsvWriter(yourTextWriterOrWhatever);
cw.Configuration.QuoteNoFields = true;
cw.WriteHeader<YourClassNameHere>();
cw.NextRecord(); // without this first row is on same line as header
cw.Configuration.QuoteAllFields = true; //or set QuoteNoFields = false
cw.WriteRecords(yourCollectionOfYourClass);
Just to be clear; you cannot paste and go on this code above, you have to edit it to be valid to your context - anywhere you encounter he word "Your" needs adjusting.
The important part I'm aiming to outline is that you should
- turn quotes off in the config,
- write the headers,
- turn quoting on (either by quoting everything or disabling the quotenofields and letting csvhelper work out whether to quote or not)
- write the data records
edited Jan 20 at 16:54
niico
3,934105397
3,934105397
answered Nov 24 '18 at 20:54
Caius JardCaius Jard
12.7k21440
12.7k21440
QuoteAllFields appears to have stopped working in version 2? It's no longer available.
– niico
Dec 20 '18 at 19:09
1
Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?
– Caius Jard
Dec 20 '18 at 19:28
1
A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…
– Caius Jard
Dec 20 '18 at 19:32
This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.
– niico
Jan 20 at 16:49
add a comment |
QuoteAllFields appears to have stopped working in version 2? It's no longer available.
– niico
Dec 20 '18 at 19:09
1
Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?
– Caius Jard
Dec 20 '18 at 19:28
1
A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…
– Caius Jard
Dec 20 '18 at 19:32
This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.
– niico
Jan 20 at 16:49
QuoteAllFields appears to have stopped working in version 2? It's no longer available.
– niico
Dec 20 '18 at 19:09
QuoteAllFields appears to have stopped working in version 2? It's no longer available.
– niico
Dec 20 '18 at 19:09
1
1
Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?
– Caius Jard
Dec 20 '18 at 19:28
Both 2.x joshclose.github.io/CsvHelper/2.x/… and 3.x joshclose.github.io/CsvHelper/configuration#writing documentation still reference it?
– Caius Jard
Dec 20 '18 at 19:28
1
1
A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…
– Caius Jard
Dec 20 '18 at 19:32
A commit 17 days ago indicates that these have indeed been removed in favor of ShouldQuote - github.com/JoshClose/CsvHelper/commit/…
– Caius Jard
Dec 20 '18 at 19:32
This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.
– niico
Jan 20 at 16:49
This works - Note you need to call NextRecord after WriteHeader - or the first row of data will be written on the same line as the header.
– niico
Jan 20 at 16:49
add a comment |
You could use something like this
cw.Configuration.ShouldQuote = (field, context) => context.HasHeaderBeenWritten;
or
cw.Configuration.ShouldQuote = (field, context) => context.Row > 1;
But you'll need to test and adjust as needed.
add a comment |
You could use something like this
cw.Configuration.ShouldQuote = (field, context) => context.HasHeaderBeenWritten;
or
cw.Configuration.ShouldQuote = (field, context) => context.Row > 1;
But you'll need to test and adjust as needed.
add a comment |
You could use something like this
cw.Configuration.ShouldQuote = (field, context) => context.HasHeaderBeenWritten;
or
cw.Configuration.ShouldQuote = (field, context) => context.Row > 1;
But you'll need to test and adjust as needed.
You could use something like this
cw.Configuration.ShouldQuote = (field, context) => context.HasHeaderBeenWritten;
or
cw.Configuration.ShouldQuote = (field, context) => context.Row > 1;
But you'll need to test and adjust as needed.
answered Dec 31 '18 at 20:51
camilohecamilohe
441512
441512
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460714%2fwrite-quotes-round-all-fields-except-first-heading-row%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why do you want to?
– Caius Jard
Nov 24 '18 at 17:34
Client requirement
– niico
Nov 24 '18 at 17:40
Show the entire code you use to write the file
– Caius Jard
Nov 24 '18 at 20:24