Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XSLT不编码双字节字符_C#_Xml_Xslt_Encoding - Fatal编程技术网

C# XSLT不编码双字节字符

C# XSLT不编码双字节字符,c#,xml,xslt,encoding,C#,Xml,Xslt,Encoding,我正在开发一个查看器,使用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://

我正在开发一个查看器,使用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>
输出HTML的内容将使用“ó”而不是“ó”

编辑:

在html字符串中的和标记之间添加此项解决了我的问题:

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

新的XmlTextWriter(ms,Encoding.ASCII)
更改为
新的XmlTextWriter(ms,Encoding.UTF8)

更新:


另一个可能的问题是,尽管XML文件有一个
encoding=“utf-8”
声明,但可能文件实际上并没有使用该编码保存。检查所有XML文件的编码是否与其声明的编码匹配。就我个人而言,我更愿意放弃声明编码,这样它就可以被自动检测到。

很确定,因为您使用了错误的编码,请尝试以下方法:

using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.Unicode))

谢谢你的快速回复。我一提交问题就发现了。我编辑了这篇文章。当更改为Encoding.UTF8时,我现在得到的是“ó”而不是“?”,但它仍然不是正确的输出。感谢您的回复,请参阅我对Jacob答案的评论。您可以始终使用十六进制:ó;换句话说,说编码错误的答案是正确的,但他们发现了错误的编码。问题是,您在提供UTF-8输出时,没有将HTML服务器配置为将其标记为UTF-8输出,因此浏览器试图将UTF-8数据读取为ISO 8859-1。这对我来说是一个两位伪造。第一个是由提供的答案所建议的(在我发布问题后的固定秒数),第二个是由我的编辑和您的评论所解释的。
 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();
         }
     }
 }
 <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>
using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.Unicode))