Do I need to escape backslash in a config file?

久未见 提交于 2019-11-29 09:04:41

You don't need that. Anything within an attribute value is character data.

Since you're reading these values using C#, they'll get escaped as if they would be a literal path string in code.

Anyway, you might want to know that C# has @ operator to declare verbatim strings, meaning that you don't need to escape backslashes when using literal paths in code:

string somePath = @"C:\blah\blih\bluh.txt";

A backslash has no special meaning in XML, so they should not be escaped.

Besides, if you would escape the backslashes in XML you would not use \\, you would use \.

The reason that it works with double backslashes also is that the file system is forgiving. You can use the path c:\\temp\\info.txt to reach the file c:\temp\info.txt.

Basically URL or URI holds single slash \ so, its better to use single slash. The problem comes while writing code, but in XML there is no problem to use single slash.

I think the best would to prevent the double backslash just in case, but if it works why change it. Maybe replace "\\" with "\" when you read the config value into your application.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!