I fixed the issues in the code below. You might integrate this solution if you want to.
Parts = new List<Part>();
Characteristics = new List<Characteristic>();
_currentPart = null;
var dfqFile = File.ReadAllLines(dfqFilePath);
var sectionBlock = new List<List<string>>();
var lineBlock = new List<string>();
Boolean addToLineblock = false;
int counter = 1;
//Create Blocks from file, then process the blocks
for (int i=0; i<dfqFile.Length; i++)
{
String previousLine = "";
String currentLine = dfqFile[i];
String nextLine = "";
if (i>0)
{
previousLine = dfqFile[i - 1];
}
if (i<dfqFile.Length-1)
{
nextLine = dfqFile[i + 1];
}
//Set the separator
//1. Empty lines
//2. K10* to K20 if there is no line break
//Create lineblock
//Console.WriteLine("Create text blocks.");
//=== Special text splitter ===//
//Check if there is a special splitter
Boolean textSplitActiveStart = false;
if (previousLine.StartsWith("K10") && currentLine.StartsWith("K20"))
{
textSplitActiveStart = true;
Console.WriteLine("Special text split start K10*->K20*");
}
//====================================//
//Stop line block
if (string.IsNullOrEmpty(currentLine) || textSplitActiveStart == true || string.IsNullOrEmpty(nextLine))
{
sectionBlock.Add(new List<String>(lineBlock));
lineBlock.Clear();
addToLineblock = false;
Console.WriteLine("End line block at line: " + counter + ">" + currentLine);
}
//Start line block
//Startpart is at start/run, previousline was empty or split K1*/K2 was active
if (string.IsNullOrEmpty(currentLine)==false && (counter==1 || string.IsNullOrEmpty(previousLine)==true || textSplitActiveStart==true))
{
addToLineblock = true;
Console.WriteLine("Start new lineblock at line: " + counter + ">" + currentLine);
}
//Add line if block is open
if (addToLineblock==true)
{
lineBlock.Add(currentLine);
Console.WriteLine("Added line to block. Line: " + counter + ">" + currentLine);
}
counter++;
}
Console.WriteLine("Created " + sectionBlock.Count() + " section blocks.");
Console.WriteLine("Process each section block.");
foreach (var block in sectionBlock)
{
if (block.Count()>0)
{
ProcessBlock(block);
}
}
Console.WriteLine("Conversion finished");
}