[英]The XslCompiledTransform output encoding
My code: 我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Xsl;
namespace xslt_samples {
class Program {
static void Main(string[] args) {
XslCompiledTransform myXslTransform = new XslCompiledTransform();
// Here the myXslTransform.OutputSettings is null still...
myXslTransform.Load(@".\in3.xsl");
// The myXslTransform.OutputSettings is not null now, but
// I get an exception: the XmlWriterSettings.Encoding read only.
myXslTransform.OutputSettings.Encoding = Encoding.UTF8;
myXslTransform.Transform(@".\in.xml", @".\out.xml");
}
}
}
The problem is pointed it the comments. 这个问题指出了评论。
How can I set the output encoding in this case? 在这种情况下如何设置输出编码?
Thank you. 谢谢。
Use 采用
XmlWriterSettings xws = myXslTransform.OutputSettings.Clone();
xws.Encoding = Encoding.UTF8;
using (XmlWriter xw = XmlWriter.Create("out.xml", xws))
{
myXslTransform.Transform(@".\in.xml", xw);
}
This is straight from the docs . 这直接来自文档 。
XslCompiledTransform.OutputSettings Property
XslCompiledTransform.OutputSettings属性
Gets an
XmlWriterSettings
object that contains the output information derived from thexsl:output
element of the style sheet.获取一个
XmlWriterSettings
对象,该对象包含从样式表的xsl:output
元素派生的输出信息。Syntax
句法
public XmlWriterSettings OutputSettings { get; }
It's a read-only property. 这是一个只读属性。
The docs go on with 文档继续
Remarks
备注
This property is populated after a successful call to the
Load
method.成功调用
Load
方法后,将填充此属性。 It contains information derived from thexsl:output
element of a compiled style sheet.它包含从已编译样式表的
xsl:output
元素派生的信息。This
XmlWriterSettings
object can be passed to theXmlWriter.Create
method to create theXmlWriter
object to which you want to output.可以将此
XmlWriterSettings
对象传递给XmlWriter.Create
方法,以创建要输出的XmlWriter
对象。
Conclusions: 结论:
XmlWriter
accepts a custom XmlWriterSettings
object. XmlWriter
接受自定义XmlWriterSettings
对象。 XslCompiledTransform
does not. XslCompiledTransform
没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.