0

我正在开发一个查看器,以使用 xslt 将 xml 日志文件显示为 html。一切都很好,除了我的本地化。生成的 HTML 文件有一个“ó”,其中一些双字节字符应该是。我无法弄清楚我做错了什么。

这是一个精简的 XSLT 文件:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions">

  <xsl:output method="html" version="4.0" encoding="utf-8" indent="yes"/>

  <xsl:variable name="language" select="nbklog/@language" />  
  <xsl:variable name="dictionaryName">
    dictionary_<xsl:value-of select="$language"/>.xml
  </xsl:variable>
  <xsl:variable name="dictionary" select="document($dictionaryName)" />

  <xsl:template match="/nbklog">
    <html>
      <body>          
        <h2>       
          <xsl:value-of select="$dictionary//String[@Key=$jobType]" /> 
        </h2>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

这是一个用于本地化的字典 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
  <Dictionary xml:lang="es-ES">
    <String Key="Application">
      Applicación
    </String>
  </Dictionary>

这是要转换的示例 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<nbklog id="51b654d4" jobType="backup" language="es-ES" version="1.0">
    <deviceName>c:\</deviceName>
    ....
</nbklog>

我正在执行以下 c# 代码的转换:

 string theOutputHtml;

 using (MemoryStream ms = new MemoryStream()) {
     using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8)) {

         XPathDocument theDocument = new XPathDocument(inXmlFilename);

         // Load the style sheet and run the transformation.
         XslCompiledTransform theXslTrasform = new XslCompiledTransform();
         theXslTrasform.Load(inXsltFilename, XsltSettings.TrustedXslt, null);
         theXslTrasform.Transform(theDocument, writer);

         ms.Position = 0;

         using (StreamReader theReader = new StreamReader(ms)) {
             theOutputHtml = theReader.ReadToEnd();
         }
     }
 }

TheOutputHtml 的内容将有一个 'ó' 而不是 'ó'。

编辑:

在 html 字符串的 and 标记之间添加这个解决了我的问题:

 <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>
4

2 回答 2

1

更改new XmlTextWriter(ms, Encoding.ASCII)new XmlTextWriter(ms, Encoding.UTF8)

更新:

另一个可能的问题是,尽管您的 XML 文件有一个encoding="utf-8"声明,但这些文件实际上可能并未使用该编码保存。检查所有 XML 文件的编码是否与其声明的编码匹配。就个人而言,我更喜欢取消声明编码,以便可以自动检测到它。

于 2013-06-20T21:40:48.450 回答
1

很确定是因为你使用了错误的编码,试试这个:

using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.Unicode))
于 2013-06-20T21:41:10.163 回答