How to read double quotes (") in a text file in C#?

How to read double quotes (") in a text file in C#?



I have to read a text file and then to parse it, in C# using VS 2010. The sample text is as follows,


[TOOL_TYPE]

; provides the name of the selected tool for programming

“Phoenix Select Advanced”;

[TOOL_SERIAL_NUMBER]

; provides the serial number for the tool

7654321;

[PRESSURE_CORRECTION]

; provides the Pressure correction information requirement

“Yes”;

[SURFACE_MOUNT]

; provides the surface mount information

“Yes”;

[SAPPHIRE_TYPE]

; provides the sapphire type information

“No”;



Now I have to parse only the string data (in double quotes) and headers (in square brackets), and then save it into another text file. I can successfully parse the headers but the string data in double quotes is not appearing correctly, as shown below.


[TOOL_TYPE]
�Phoenix Select Advanced�;
[TOOL_SERIAL_NUMBER]
7654321;
[PRESSURE_CORRECTION]
�Yes�;
[SURFACE_MOUNT]
�Yes�;
[SAPPHIRE_TYPE]
�No�;
[EXTENDED_TELEMETRY]
�Yes�;
[OVERRIDE_SENSE_RESISTOR]
�No�;



Please note a special character (�) which is appearing every time whenever a double quotes appear.



How can I write the double quotes(") in the destination file and avoid (�) ?



I am using the following line for my parsing



temporaryconfigFileWriter.WriteLine(configFileLine, false, Encoding.Unicode);



Here is the complete code I am using:


string temporaryConfigurationFileName = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\Temporary_Configuration_File.txt";

//Pointers to read from Configuration File 'configFileReader' and to write to Temporary Configuration File 'temporaryconfigFileWriter'
StreamReader configFileReader = new StreamReader(CommandLineVariables.ConfigurationFileName);
StreamWriter temporaryconfigFileWriter = new StreamWriter(temporaryConfigurationFileName);

//Check whether the 'END_OF_FILE' header is specified or not, to avoid searching for end of file indefinitely
if ((File.ReadAllText(CommandLineVariables.ConfigurationFileName)).Contains("[END_OF_FILE]"))

//Read the file untill reaches the 'END_OF_FILE'
while (!((configFileLine = configFileReader.ReadLine()).Contains("[END_OF_FILE]")))

configFileLine = configFileLine.Trim();
if (!(configFileLine.StartsWith(";")) && !(string.IsNullOrEmpty(configFileLine)))

temporaryconfigFileWriter.WriteLine(configFileLine, false, Encoding.UTF8);


// to write the last header [END_OF_FILE]
temporaryconfigFileWriter.WriteLine(configFileLine);

configFileReader.Close();
temporaryconfigFileWriter.Close();





you need to provide the source you use for reading/parsing/writing... I suspect there is something off with the Encodings you use along the way...
– Yahia
Dec 4 '11 at 12:37





Show us the code you use to write text to the output file..
– Shai
Dec 4 '11 at 12:38




2 Answers
2



Your input file doesn't contain double quotes, that's a lie. It contains the opening double quote and the closing double quote not the standard version.



First you must ensure that you are reading your input with the correct encoding (Try multiple ones and just display the string in a textbox in C# you'll see if it show the characters correctly pretty fast)



If you want such characters to appear in your output you must write the output file as something else than ASCII and if you write it as UTF-8 for example you should ensure that it start with the Byte Order Mark (Otherwise it will be readable but some software like notepad will display 2 characters as it won't detect that the file isn't ASCII).



Another choice is to simply replace and with "




"





see also fileformat.info/info/unicode/char/201c/index.htm and fileformat.info/info/unicode/char/201d/index.htm
– wimh
Dec 4 '11 at 12:45





Yes, sorry to put it wrongly. My file has these opening and closing quotes for string identification. Then how can i ensure to write it properly in the other text file. Please see my eidted explanation for the encoding I am using and suggest me solution.
– Asad
Dec 4 '11 at 12:50





@Asad: That's why I said yo check with a small software that just load the text in a textbox. UTF-16 encoded text files are really rare, most of the time it's UTF-8 on the disk (Encoding.UTF8)
– Julien Roncaglia
Dec 4 '11 at 12:53


Encoding.UTF8





I used all the four encoding ASCII, UNICODE, UTF-8 and UTF-16 but none of them is showing the correct answer. And yes I am writing the data as a (notepad) text file.
– Asad
Dec 4 '11 at 12:57





@asad You will either need to provide more informations or debug by yourself. See this code : pastebin.com/BJRLXciu it works when using your text placed in an UTF-8 text file. So the problem is either somewhere else in your code, not in the loading/saving or you need to find the encoding of your input.
– Julien Roncaglia
Dec 4 '11 at 13:12



It appears that you are using proper typographic quotes (“...”) instead of the straight ASCII ones ("..."). My guess would be that you read the text file with the wrong encoding.


“...”


"..."



If you can see them properly in Notepad and neither ASCII nor one of the Unicode encodings works, then it's probably codepage 1252. You can get that encoding via


Encoding.GetEncoding(1252)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj