我正在尝试在源xml上应用XSL样式表,并将输出写入目标xml文件。 xsl删除源xml中存在的xml注释。
目标xml文件在标头中包含UTF-16编码。
但我仍然希望输出xml是utf-8编码。我使用的代码是
XmlWriterSettings xwrSettings = new XmlWriterSettings();
**xwrSettings.Encoding = Encoding.UTF8;**
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("sample.xsl");
StringBuilder sb = new StringBuilder();
XmlReader xReader = XmlReader.Create("Source.xml");
XmlWriter xWriter = XmlWriter.Create(sb, xwrSettings);
xslt.Transform(xReader, xWriter);
File.WriteAllText("Target.xml",sb.ToString());
我尝试将xml writer设置为UTF-8,但它无效。
答案 0 :(得分:1)
由于您正在写入文件,为什么不使用:
using (XmlReader xReader = XmlReader.Create("Source.xml"))
using (XmlWriter xWriter = XmlWriter.Create("Target.xml", xwrSettings)) {
xslt.Transform(xReader, xWriter);
}
// your file is now written