1

I am writing data in CSV and reading the line from CSV in C#. When I am writing data -

using StreamWriter streamWriter = new StreamWriter(outputFilePath);
using var csv = new CsvWriter(streamWriter, CultureInfo.InvariantCulture);

In CSV file data looks like this

70ea2d19-1776-4f25-b564-9eecf0a857df<->{"eeea3add-4e78-4a70-b674-1d

When I am reading data -

sing (StreamReader streamReader = new StreamReader(filePath))
            {
                string currentLine;
                while (!string.IsNullOrWhiteSpace(currentLine = streamReader.ReadLine()))
                {.
.
.
.

I get the line as

"\"70ea2d19-1776-4f25-b564-9eecf0a857df<->{\"\"eeea3add-4e78-4a70-b674-1d55ddd2165e\"\":[{\

How to get rid of these special characters? Regex.Unescape didnt work.

  • 1
    "I get the line as" - where **exactly** are you seeing that? if you're in the IDE: the IDE **lies** - the debugger shows escaped values *for your convenience* (so that a: you can see non-printing characters, and b: you can copy/paste it as code). Knowing exactly how you're looking at the value: matters – Marc Gravell May 19 '22 at 07:28
  • I need to get the value and deserialize further. So I set debugger at deserialize line. JsonConvert.DeserializeObject call fails due to these special chars inside string. – Kavita Korgaonkar May 19 '22 at 07:32
  • 1
    That doesn't answer the question, though; are you looking at `currentLine` in the debugger? if so: ignore the `\"` escaping - that *doesn't exist*, and `\"` just means: `"`; however, the value you show does suggest (in the `\"\"`) that it has two sets of quotes, i.e. `""`; the real question to me is: what is actually in the file? Perhaps even at the byte level. You suggest that in the CSV file the data is single `"`; are you absolutely sure about that? – Marc Gravell May 19 '22 at 07:36
  • If you are using the VS debugger, try clicking the magnification glass next to the string, that should show the un-escaped string. – JonasH May 19 '22 at 07:44
  • Yes, I just checked the content inside CSV file. – Kavita Korgaonkar May 19 '22 at 07:47
  • If you wrote csv using CsvWriter, then you should also read csv using CsvReader. – Alexander Petrov May 19 '22 at 11:22
  • Show exactly how you wrote the csv. – Alexander Petrov May 19 '22 at 11:23

0 Answers0