Search code examples
c#.netutf-8utf-16utf

Convert file path to UTF-8


I want to get, print and write to a text file the full path on disk of a file named A&T+X-8_L_R1.png but when I print it I get A&T+X-8_L_R1.png.

AFAIK I need to change the encoding. I did a search and found this potential solution but it doesn't work:

String filePathString = relativeUri.ToString();

byte[] bytes = Encoding.Default.GetBytes(filePathString);
filePathString = Encoding.UTF8.GetString(bytes);
filePathNode.SetValue(filePathString);

This is the full code of my class: http://pastebin.com/dZLGeS8p

The class searches recursively for *.png files and creates an XML structure from their paths. When I save the XML file the special characters from the paths like & are changed.

Can anyone point me to a solution?


Solution

  • You are writing an XML file, not a plain text file. In XML, an ampersand needs to be escaped to &.

    So the result you get is perfectly ok. It's even required to be like this.

    I recommend to open the XML file with an application that can properly validate and display XML. It'll be easier to see that the file is correct.

    The UTF-8 conversion in your code isn't required. If the XML file is encoded in UTF-8, your XML classes will take care of any required conversions.