Replies: 3 comments
-
Not exactly pretty, but something like this might work to combine the streams before reading. using (var stream = new MemoryStream())
using (var reader = new StreamReader(stream))
using (var writer = new StreamWriter(stream))
using (var csv = new CsvReader(reader))
using (var headerReader = new StreamReader("path\\to\\Header.csv"))
using (var recordReader = new StreamReader("path\\to\\Record.csv"))
{
headerReader.BaseStream.CopyTo(stream);
writer.WriteLine();
writer.Flush();
recordReader.BaseStream.CopyTo(stream);
stream.Position = 0;
var records = csv.GetRecords<dynamic>().ToList();
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Unfortunately that will load the entire file into memory, which could be bad if the file is large. Another way is to manipulate the context directly. void Main()
{
var headerReader = new StringReader("Id,Name\r\n");
var contentReader = new StringReader("1,one\r\n");
string[] header;
using (var csv = new CsvParser(headerReader))
{
header = csv.Read();
}
using (var csv = new CsvReader(contentReader))
{
csv.Configuration.HasHeaderRecord = false;
csv.Context.HeaderRecord = header;
csv.GetRecords<Foo>().Dump();
}
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
@AltruCoder You've been answering a lot of questions lately and I want to say thank you. It is tremendously helpful for me. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a CSV file without headers. The headers are located in a separate 1-line CSV file.
These are all dynamic so can't be mapped to a class.
Is there a way to pass CsvReader a list of headers to be used during parsing and construction of the ExpandoObjects?
Beta Was this translation helpful? Give feedback.
All reactions