title: Encode Xml in iso-8859-15 to Plain Text in Utf8 and then Decode Back
body: <p>I have a xmlDocument in iso-8859-15 for Portuguese specific characters.
  I need to encode the xml to plain text into a txt file that I will upload to a remote server.</p>
  
  <p>Then I need to download the file and put it back to Xml without losing my special characters.</p>
  
  <p>I was trying with base64 encoding, but I always lose the characters even when I specify the encoding.</p>
  
  <pre><code>Encoding encoding = Encoding.GetEncoding("ISO-8859-15");
  string _decrypted =    GlobalProcedures.base64Decode(Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(GlobalVariables.SpiroStockManagmentDatabaseProcedures.XmlDatabaseFilePath + "encrypted2", encoding));
  </code></pre>
  
  <p>The Xml looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="iso-8859-15"?&gt;
  &lt;InventoryList&gt;
    &lt;Product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;Id&gt;2&lt;/Id&gt;
      &lt;Name&gt;Pão Hamburger Bimbo C/sésamo&lt;/Name&gt;
      &lt;Price&gt;1.38&lt;/Price&gt;
      &lt;VariableWeightPrice&gt;6,27/kg&lt;/VariableWeightPrice&gt;
      &lt;Brand&gt;BIMBO&lt;/Brand&gt;
      &lt;PackageInfo&gt;4UN 220GR&lt;/PackageInfo&gt;
      &lt;categoryString&gt;Mercearia Salgada&lt;/categoryString&gt;
      &lt;PictureSmallFilename&gt;2small.jpg&lt;/PictureSmallFilename&gt;
      &lt;InformationTakenFrom&gt;Jumbo&lt;/InformationTakenFrom&gt;
      &lt;MarketItemUrl&gt;http://www.jumbo.pt/Frontoffice/ContentPages/CatalogProduct.aspx?id=40733&lt;/MarketItemUrl&gt;
      &lt;BarCode&gt;5601009920037&lt;/BarCode&gt;
      &lt;IsBlackListed&gt;false&lt;/IsBlackListed&gt;
      &lt;InsertDate&gt;2011-10-09T16:45:25&lt;/InsertDate&gt;
      &lt;QuantityIn&gt;0&lt;/QuantityIn&gt;
      &lt;QuantityWeightIn&gt;0&lt;/QuantityWeightIn&gt;
      &lt;QuantityOut&gt;0&lt;/QuantityOut&gt;
      &lt;QuantityWeightOut&gt;0&lt;/QuantityWeightOut&gt;
      &lt;History&gt;
        &lt;Item&gt;
          &lt;Quantity&gt;1&lt;/Quantity&gt;
          &lt;QuantityWeight&gt;0&lt;/QuantityWeight&gt;
          &lt;ListName&gt;out&lt;/ListName&gt;
          &lt;InsertDate&gt;2011-11-08T07:57:45&lt;/InsertDate&gt;
        &lt;/Item&gt;
        &lt;Item&gt;
          &lt;Quantity&gt;1&lt;/Quantity&gt;
          &lt;QuantityWeight&gt;0&lt;/QuantityWeight&gt;
          &lt;ListName&gt;in&lt;/ListName&gt;
          &lt;InsertDate&gt;2011-12-06T23:09:21&lt;/InsertDate&gt;
        &lt;/Item&gt;
      &lt;/History&gt;
    &lt;/Product&gt;
  &lt;/InventoryList&gt;
  </code></pre>
  
  <p>Is this possible? Thank you</p>
  
  <p>edited ( here´s the code ):</p>
  
  <pre><code>//read and encrypt
              string _encrypted2 = GlobalProcedures.base64Encode((Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(GlobalVariables.SpiroStockManagmentDatabaseProcedures.XmlDatabaseFilePath)));
              FileInfo fileInfo2 = new FileInfo(GlobalVariables.SpiroStockManagmentDatabaseProcedures.XmlDatabaseFilePath + "encrypted2");
              FileStream stream2 = fileInfo2.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
          StreamWriter m_streamWriter2 = new StreamWriter(stream2, encoding);
          try
          {
              m_streamWriter2.Write(_encrypted2);
          }
          finally
          {
              m_streamWriter2.Close();
              stream2.Close();
          }
  
              //read and decrypt and right to other file
              string _decrypted = GlobalProcedures.base64Decode(Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(GlobalVariables.SpiroStockManagmentDatabaseProcedures.XmlDatabaseFilePath + "encrypted2", encoding));
                 GlobalVariables.SpiroStockManagmentDatabaseProcedures.LoadXmlDocumentFromString(_decrypted);
  
              FileInfo fileInfo2 = new FileInfo(GlobalVariables.SpiroStockManagmentDatabaseProcedures.XmlDatabaseFilePath + "decrypted2");
              FileStream stream2 = fileInfo2.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
              StreamWriter m_streamWriter2 = new StreamWriter(stream2, encoding);
              try
              {
                  m_streamWriter2.Write(_decrypted);
              }
              finally
              {
                  m_streamWriter2.Close();
                  stream22.Close();
              }
  </code></pre>
  
  <p>And here´s the code to encode in base64</p>
  
  <pre><code>static public string base64Encode(string data)
      {
          try
          {
              byte[] encData_byte = new byte[data.Length];
              encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
              string encodedData = Convert.ToBase64String(encData_byte);
              return encodedData;
          }
          catch (Exception e)
          {
              throw new Exception("Error in base64Encode" + e.Message);
          }
      }
  
      static public string base64Decode(string data)
      {
          try
          {
              System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
              System.Text.Decoder utf8Decode = encoder.GetDecoder();
  
              byte[] todecode_byte = Convert.FromBase64String(data);
              int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
              char[] decoded_char = new char[charCount];
              utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
              string result = new String(decoded_char);
              return result;
          }
          catch (Exception e)
          {
              throw new Exception("Error in base64Decode" + e.Message);
          }
      }
  </code></pre>
  
  <p>EDITED : </p>
  
  <p>THank you for your awnsers. Douglas. I don´t thing i can remove the encoding="iso-8859-15" from the xml file. Because i use Xml serialization. And without it, the portuguese specific characters dissapear. –</p>