Badr Hari
Badr Hari

Reputation: 8384

Set custom encoding while saving a file to isolated storage

I am saving a downloaded file to isolated storage, I would like to set the encoding to iso-8859-1 since the xml file I'm downloading is encoded that way.

var stream = new IsolatedStorageFileStream("myfile.xml", FileMode.Create, FileAccess.Write, myIsolatedStorage);
        using (StreamWriter writeFile = new StreamWriter(stream)) {
            string xml_file = e.Result.ToString();
            writeFile.WriteLine(xml_file);
            writeFile.Close();
        }

Upvotes: 0

Views: 342

Answers (2)

Pedro Lamas
Pedro Lamas

Reputation: 7233

The ideal solution would be to download the file as a stream and saving it directly to file, thus maintaining the downloaded bytes as is (so no text encoding conversion would be required)!

Also, Windows Phone only supports a few basic text encodings like UTF8 and Unicode; in order to use another encoding in WP, you'll have to use the Silverlight Text Encoding Generator tool!

Upvotes: 3

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

You are converting result to string so you are writing UTF-16 into the stream. As result there is no way to match encoding in stream and XML. You may be able to copy response stream to isolated storage stream.

Please do not use string manipulation to construct, read and write XML. There are plenty corresponding classes to do it properly. In your case you likely want to read XML with either XmlDocument or XDocument and than save to the stream optionally specifying encoding when creating XmlWriter.

Upvotes: 2

Related Questions