0

I'm using a WCF to unzip a file that I receive from an API (that I cant modifie). The Zip file that I receive contains XML files that are not encoded. enter image description here

The problem is that I cant Deserialise these files later (I can't even load them to a XmlDocument). I want to do one of these two:
1. Add the encoding when extracting the zip file,
2. Or add the encoding to the file before reading it later (just before deserializing it)
This is a pick from the code when extracting the zip file:

var subStream = entry.OpenReader();
Directory.CreateDirectory(subPath);
subPath +="\\" + entryPath.Last();
using (var output = new FileStream(subPath, FileMode.OpenOrCreate))
   {
       await entryStream.CopyToAsync(output);
   }

PS: Adding the encoding o the entry doesn't modifie the encoding when the file is saved.

Thank you.

Hodyr
  • 53
  • 7
  • what do you mean by ' Add encoding' – Lei Yang Mar 10 '17 at 08:44
  • Maybe you should try this: http://stackoverflow.com/questions/961699/how-to-change-character-encoding-of-xmlreader – daniell89 Mar 10 '17 at 08:44
  • Can you use StreamWriter instead of FileStream and add Encoding.UTF8 or whatever enconding you want? http://stackoverflow.com/questions/23168325/changing-filestream-write-encoding-type – Kenci Mar 10 '17 at 08:45
  • There is no "not encoded" for text-based files. String representation *always* needs an encoding. I guess it will be UTF-8 if nothing is stated explicitly. "I can't even load them to a XmlDocument" - what does that mean? Do you get an exception? If so, show the stacktrace please. – Fildor Mar 10 '17 at 08:53
  • @Fildor this is the stacktrace: http://imgur.com/a/JXWms – Hodyr Mar 10 '17 at 09:02
  • @Kenci I tried to use StreamWriter because it has and Encoding option but it writes strings not streams – Hodyr Mar 10 '17 at 09:03
  • @Fildor this is the text of the exception: '.', valeur hexadécimale 0x00, est un caractère non valide. Ligne 2, position 1. – Hodyr Mar 10 '17 at 09:05
  • FileStream output = new FileStream(subPath, FileMode.OpenOrCreate); using (var output1 = new StreamReader(output, Encoding.UTF8)) { } – jdweng Mar 10 '17 at 10:21
  • @MehdiSayeb Convert the XML string in the file with: byte[] bytes = Encoding.Default.GetBytes(myString); myString = Encoding.UTF8.GetString(bytes); and use XDocument.Parse(myString); ? – Kenci Mar 10 '17 at 11:44
  • @Kenci I can't get the XML string because I can't do anything with a not encoded stream. – Hodyr Mar 10 '17 at 13:33

0 Answers0