简体   繁体   English

XslCompiledTransform输出编码

[英]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 the xsl: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 the xsl:output element of a compiled style sheet. 它包含从已编译样式表的xsl:output元素派生的信息。

This XmlWriterSettings object can be passed to the XmlWriter.Create method to create the XmlWriter object to which you want to output. 可以将此XmlWriterSettings对象传递给XmlWriter.Create方法,以创建要输出的XmlWriter对象。

Conclusions: 结论:

  • The XmlWriter accepts a custom XmlWriterSettings object. XmlWriter接受自定义XmlWriterSettings对象。
  • The XslCompiledTransform does not. XslCompiledTransform没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM