Search code examples
c#asp.net-mvcstreamreaderstreamwriter

Streamwriter is delimiting by all commas and I need it to stop


I have a 'translate content' application. Basically, a user uses Summernote text editor to put content (HTML) into the system. They can then request a translation by downloading a csv file with all the information in it to send to the translator. When we get it back from the translator, we will upload the csv file that contains the translations.

Issue 1: Some of the columns are content based and have commas in them. The same content is also in HTML format. So you may have:

<p style: "color: blue;"> I am a test, and I like to cause problems </p>

Issue 2: When I re-import the file, it delimits by semi-colon. So everything in the in-line styling is broken up.

The Code to make the CSV:

Response.ContentType = "text/plain";
Response.AddHeader("content-disposition", "attachment;filename=" + downloadName);
Response.Clear();
   using (StreamWriter contentWriter = new StreamWriter(Response.OutputStream, Encoding.UTF8))
   {
       contentWriter.WriteLine("Application ID, English ID, Title, Application, Content, Translate To, TRANSLATE HERE, Requested By, Request Date");

                foreach (translation_contents content in toBeTranslatedContents)
                {
                    Guid parentKey = content.translation_contentid;
                    string title = string.Format(" \"{0} \" ", content.ContentTitle);
                    string englishContent = string.Format(" \"{0} \" ", content.Content);


                    contentWriter.WriteLine(applicationKey + "," + parentKey + "," + title + "," + applicationName + "," + englishContent + "," + newLanguage + "," + englishContent + "," + requestedBy + "," + requestDate);
                }
     }

      Response.End();

What I have tried:

string englishContent = string.Format(" \"{0} \" ", content.Content);

Everything said that this should work. It does, in fact, add quotes around the content. However, it still delimits by the commas. So I'll get two columns such as

col 1: "I am a test
col 2: and I like to cause problems"

Where the quotes are there, but it's still broken up. I also tried:

string englishContent = content.Content.Replace(",", "&apos;");

This works very well on export and can still be understood and rendered by SummerNote Text Editor. The issue is that when I re-import it, the semi-colons are wreaking havoc and breaking the content up into 5-10 columns rather than just the one full piece of content.

Code To Import:

using (StreamReader fileReader = new StreamReader(path))
{
    List<string> listA = new List<string>();
    while (!fileReader.EndOfStream)
    {
        var line = fileReader.ReadLine();
        var values = line.Split(';');

         listA.Add(values[0]);
     }
  }

Thoughts on how to just keep content as a single block and not break it up or delimit it in anyway?

Thank you in advance!


Solution

  • The answer by Panagiotis Kanavos solved my issue. Using CSVhelper, I was able to export and import CSVs with ease.