将iso-8859-15中的Xml编码为Utf8中的纯文本,然后重新编码

时间:2012-02-03 19:24:19

标签: c# .net xml encoding

我在ISO-8859-15中有一个xmlDocument用于葡萄牙语特定字符。 我需要将xml编码为纯文本到txt文件中,我将上传到远程服务器。

然后我需要下载文件并将其放回Xml而不会丢失我的特殊字符。

我尝试使用base64编码,但即使我指定了编码,我也总是丢失字符。

Encoding encoding = Encoding.GetEncoding("ISO-8859-15");
string _decrypted =    GlobalProcedures.base64Decode(Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(GlobalVariables.SpiroStockManagmentDatabaseProcedures.XmlDatabaseFilePath + "encrypted2", encoding));

Xml看起来像这样:

<?xml version="1.0" encoding="iso-8859-15"?>
<InventoryList>
  <Product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Id>2</Id>
    <Name>Pão Hamburger Bimbo C/sésamo</Name>
    <Price>1.38</Price>
    <VariableWeightPrice>6,27/kg</VariableWeightPrice>
    <Brand>BIMBO</Brand>
    <PackageInfo>4UN 220GR</PackageInfo>
    <categoryString>Mercearia Salgada</categoryString>
    <PictureSmallFilename>2small.jpg</PictureSmallFilename>
    <InformationTakenFrom>Jumbo</InformationTakenFrom>
    <MarketItemUrl>http://www.jumbo.pt/Frontoffice/ContentPages/CatalogProduct.aspx?id=40733</MarketItemUrl>
    <BarCode>5601009920037</BarCode>
    <IsBlackListed>false</IsBlackListed>
    <InsertDate>2011-10-09T16:45:25</InsertDate>
    <QuantityIn>0</QuantityIn>
    <QuantityWeightIn>0</QuantityWeightIn>
    <QuantityOut>0</QuantityOut>
    <QuantityWeightOut>0</QuantityWeightOut>
    <History>
      <Item>
        <Quantity>1</Quantity>
        <QuantityWeight>0</QuantityWeight>
        <ListName>out</ListName>
        <InsertDate>2011-11-08T07:57:45</InsertDate>
      </Item>
      <Item>
        <Quantity>1</Quantity>
        <QuantityWeight>0</QuantityWeight>
        <ListName>in</ListName>
        <InsertDate>2011-12-06T23:09:21</InsertDate>
      </Item>
    </History>
  </Product>
</InventoryList>

这可能吗?谢谢

编辑(这是代码):

//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();
            }

这是在base64中编码的代码

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);
        }
    }

已编辑:

感谢你的笨蛋。道格拉斯。我不能从xml文件中删除encoding =“iso-8859-15”。因为我使用Xml序列化。没有它,葡萄牙语的特定字符消失了。 -

0 个答案:

没有答案