[英]xmltextwriter transforms special characters
I get special characters transformed in the result of an xslt file transformation. 我在xslt文件转换的结果中转换了特殊字符。
Has anyone experienced this before? 有谁之前经历过这个吗?
In the source document there's a character &
which in the result is presented as &
在源文档中有一个&
字符&
其结果显示为&
. 。 I need the the original &
character even in the result. 即使结果,我也需要原始字符&
字符。
XmlDataDocument dd = new XmlDataDocument(ds);
XsltSettings settings = new XsltSettings();
settings.EnableDocumentFunction = true;
settings.EnableScript = true;
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(XmlReader.Create(new StringReader(transformSource.Transform)), settings, new XmlUrlResolver());
XsltArgumentList a = new XsltArgumentList();
a.AddExtensionObject("http://www.4plusmedia.tv", new TransformationHelper());
using (XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
transform.Transform(dd, a, writer);
}
Your output part is using (XmlTextWriter writer = ...) { ... }
您的输出部分正在using (XmlTextWriter writer = ...) { ... }
This indicates that your output is XML. 这表明您的输出是 XML。 You could use XSLT to produce plain text, that would be different. 您可以使用XSLT生成纯文本,这会有所不同。
For XML and HTML output the &
对于XML和HTML输出, &
encoding is necessary and essential. 编码是必要且必不可少的。
At some stage the Value of your Xml elements will be used and that is where (and when) &
在某个阶段,将使用您的Xml元素的值,这是(和何时) &
becomes a &
again. 再次成为&
。
In the source document there's a character & which in the result is presented as
&
在源文档中有一个&字符,其结果显示为&
. 。
Don't panic as there is no problem : this is exactly the same character &
, as it should be presented in any well-formed XML document. 不要惊慌,因为没有问题 :这与字符&
完全相同,因为它应该在任何格式良好的XML文档中显示。
You can see that this is exactly the same character, get the string value of the node and output it -- you'll see that just &
is output. 您可以看到这是完全相同的字符,获取节点的字符串值并输出它-您将看到输出&
。
Another way to ascertain that this is just the &
character is in XSLT to output it when the output method is set to "text". 另一种确定这只是&
字符的方法是在XSLT中将输出方法设置为“ text”时将其输出。 Here is a small, complete example: 这是一个小的完整示例:
XML document : XML文档 :
<t>M & M</t>
Transformation : 转型 :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Result : 结果 :
M & M
如果希望XslCompiledTransform作为XSLT转换的结果输出纯文本文件,则不应转换为创建的XmlTextWriter,而应转换为FileStream或TextWriter 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.