我必须阅读一个文本文件,然后使用VS 2010在C#中解析它.示例文本如下,
[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”;
Run Code Online (Sandbox Code Playgroud)
现在我只需解析字符串数据(双引号)和标题(方括号[]),然后将其保存到另一个文本文件中.我可以成功解析标题,但双引号中的字符串数据显示不正确,如下所示.
[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?;
Run Code Online (Sandbox Code Playgroud)
请注意每当出现双引号时出现的特殊字符( ).
如何在目标文件中编写双引号(")并避免( )?
我使用以下行进行解析
temporaryconfigFileWriter.WriteLine(configFileLine,false,Encoding.Unicode);
这是我正在使用的完整代码:
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();
}
Run Code Online (Sandbox Code Playgroud)