Skip to content

Reading multiple data sets with no blank line between them #1980

Answered by JoshClose
yooakim asked this question in Q&A
Discussion options

You must be logged in to vote

You just need to test if the line is a header. Here is an example using the data you provided.

void Main()
{
    var s = new StringBuilder();
    s.Append("FooId,Name\r\n");
    s.Append("1,one\r\n");
    s.Append("BarId,Name\r\n");
    s.Append("07a0fca2-1b1c-4e44-b1be-c2b05da5afc7,bar\r\n");
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
    };
    using (var reader = new StringReader(s.ToString()))
    using (var csv = new CsvReader(reader, config))
    {
        var foos = new List<Foo>();
        var bars = new List<Bar>();
        while (csv.Read())
        {
            if (csv[0] == "FooId" || csv[0] == "BarId")
            {
                csv.ReadHeader(

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@yooakim
Comment options

Answer selected by yooakim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants