Search code examples
c#asp.netstreamreader

StreamReader storing empty lines in list if string contains special characters


The textfile contains hundreds of lines like this and almost all of them are stored in the list. But the lines containing characters such as '-', '&' or dots ('.') are added to the list as empty lines. Out of the lines below: the 1st, 2nd, 7th, 8th, 10th & 11th lines will be stored right, the others will be stored blank. Afterwards, when i try to load the list into a dropdownlist, there's blank values inside the dropdownlist as well.

Code:

string singleLineSchedule;
List<string> courses = new List<string>();

public List<string> LoadStudentCourses()
{
try
{
    fileLoader = new StreamReader(@"C:\TestSchedule.txt", Encoding.Default, true);

    //Read one line at a time
    while ((singleLineSchedule = fileLoader.ReadLine()) != null)
    {
        //Will store any text from each line that is in between '> & </option>, for example: '>Course1</option>
        courses.Add(GetTextBetween(singleLineSchedule, "'>", "</option>"));
    }
}
catch
{
    courses.Add("Error loading schedule file");
}

fileLoader.Dispose();

return courses;
}

public static String GetTextBetween(String source, String leftWord, String rightWord)
{
return Regex.Match(source, String.Format(@"{0}(?<words>[\w\s]+){1}", leftWord, rightWord), RegexOptions.IgnoreCase).Groups["words"].Value;
}

Part of the textfile where it gets the information(they're all option types):

 <option>year 1</option>
 <option>second year</option>
 <option>class 1 containing a special-character</option>
 <option>class 2 containing special-characters & such</option>
 <option>class 3 containing a special-character</option>
 <option>class 4 containing a special-character</option>
 <option>third year</option>
 <option>fourth year</option>
 <option>fifth year with a dot.</option>
 <option>value 4</option>
 Biology</option>

Solution

  • \w\s in regex doesn't allow other characters but a-z,A-Z,0-9, white-space, so the return value of GetTextBetween is empty if there's something else in the string.