1

I am completely new to LINQ and I want to learn it's usefulness in parsing text files, instead of using a scripting language such as Perl or Ruby. I have generated a long list delimited by "CR" and "LF" and I would like to create a .CSV file to export to Excel containing only the first three lines. Example:

[CR][LF]
      Field: Microsoft.VSTS.Build.FoundIn[CR][LF]
      Name: Found In[CR][LF]
      Type: String[CR][LF]
      Use: Test project[CR][LF]
      Indexed: False[CR][LF]
      Reportable As: dimension[CR][LF]
      Synchronizes Identity Name Changes: False[CR][LF]
[CR][LF]
      Field: Microsoft.VSTS.Build.IntegrationBuild[CR][LF]
      Name: Integration Build[CR][LF]
      Type: String[CR][LF]
      Use: Test project[CR][LF]
      Indexed: False[CR][LF]
      Reportable As: dimension[CR][LF]
      Synchronizes Identity Name Changes: False[CR][LF]
[CR][LF]

(the list goes on)

Desired output:

"Microsoft.VSTS.Build.FoundIn","Found In","String"
"Microsoft.VSTS.Build.IntegrationBuild","Integration Build","String"

How can I write this in LINQ the simplest way possible?

1 Answer 1

4

You're just breaking between "sections" on blank lines. Just go through the lines throwing everything into a separate buffer until you reach a blank line. When you do, process the items in the buffer, clear it and repeat. You can't really do this in pure LINQ simply or elegantly so don't try to force feed it into it.

var buffer = new List<string>();
foreach (var line in File.ReadLines(pathToFile))
{
    if (String.IsNullOrWhitespace(line))
    {
        ProcessSection(outputFile, buffer);
        buffer.Clear(); // or create a new one
    }
    else
    {
        buffer.Add(line);
    }
}

static void ProcessSection(StreamWriter outputFile, List<string> buffer)
{
    if (buffer.Count == 0) return;
    var contents = buffer.Take(3)
        .Select(line => String.Format("\"{0}\"", line.Substring(line.IndexOf(": ") + 2)));
    outputFile.WriteLine(String.Join(",", contents));
}
Sign up to request clarification or add additional context in comments.

4 Comments

Didn't know about File.EnumerateLines - that's pretty neat.
System.IO.File does not contain EnumerateLines in .NET 4.0!
@JFB: Oops, sorry, wrong name. That should be ReadLines().
@Lester: Warning, I botched the name of that method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.