Replace the WriteLines with AppendLine instructions:
StringBuilder builder = new StringBuilder();
foreach (var item in projectToWrite)
{
builder.AppendLine(GetMSBuildFileHeader());
builder.AppendLine(GetMSBuildFileItem(item, compilerOutputPath));
And so forth.
NOTE: I moved the string builder outside the loop, so it assembles data for all strings.
Then, at the end:
return builder.ToString();