2012-01-30 47 views
0

我想发布文章在网站上的Word作为html,我有一个Windows客户端,将文章转换为HTML并将HTML文件发送到网站上的文件夹,我然后在IFrame中显示文章。但在IE9中,图像不会显示,因为IE9会尝试将它们转换为矢量图形。我决定从负责这个的HTML中删除代码,这里开始我的问题。在我修改并保存文件后,我收到了垃圾字符,这些字符也显示在网页上。但是,如果我手动编辑记事本++中的文件,我不会得到相同的问题,我怎样才能读取保存在word中的文件作为使用C#的HTML,而不是得到这些垃圾字符?这里是我的代码C#文件流无法正确读取word的文字

private bool AdjustHtmlPageForIE9Images(FileInfo file) 
    { 
     bool success = true; 
     try 
     { 
      string content = File.ReadAllText(file.FullName); 
      //replace [if gte vml 1] with [if gte iesucksopd 1] 
      content = content.Replace("[if gte vml 1]", "[if gte iesucksopd 1]"); 
      //replace [if !vml] with [if !iesucksopd] 
      content = content.Replace("[if !vml]", "[if !iesucksopd]"); 
      //now write the file over 
      File.WriteAllText(file.FullName, content); 
     } 
     catch (Exception ex) 
     { 

      throw ex; 
     } 
     return success; 
    } 

,这会导致显示一些垃圾字符。

嗨,大家好感谢这里所有的答复是我做过什么来解决这个

嗨,大家好感谢您的答复finaly得到它去,我在FF打开查看编码,它是西方Windows的1252,那么在SLaks sed中传递GetEncoding(1252)的读写操作就是修改后的代码。

private bool AdjustHtmlPageForIE9Images(FileInfo file) 
    { 
     bool success = true; 
     try 
     { 
      Encoding encoding = Encoding.GetEncoding(1252); 
      string content = File.ReadAllText(file.FullName,encoding); 
      //replace [if gte vml 1] with [if gte iesucksopd 1] 
      content = content.Replace("[if gte vml 1]", "[if gte iesucksopd 1]"); 
      //replace [if !vml] with [if !iesucksopd] 
      content = content.Replace("[if !vml]", "[if !iesucksopd]"); 
      //now write the file over 
      File.WriteAllText(file.FullName, content, encoding); 
     } 
     catch (Exception ex) 
     { 

      throw ex; 
     } 
     return success; 
    } 

是不是只是可笑的是IE9不能做这样一个简单的事情显示HTML代码字在iframe难怪它的受欢迎程度持续下降。

+1

永远不要写'抛出'。 – SLaks 2012-01-30 23:33:03

+0

该文件是什么编码? – SLaks 2012-01-30 23:33:26

+0

如何找出文件编码? – smoothe 2012-01-30 23:44:20

回答

1

您需要明确地将编码传递给ReadAllTextWriteAllText;否则,它将默认为UTF8。通过。

0

确保转换后的html文件是UTF-8或UTF-32编码,然后ReadAllText会正确检测到它。否则,使用ReadAllText重载为转换使用的编码提供参数。