XslCompiledTransform uses UTF-16 encoding

后端 未结 2 470
梦如初夏
梦如初夏 2021-01-11 15:26

I have the following code, which I want to output xml data using the UTF-8 encoding format. but it always outputs data in UTF-16 :

        XslCompiledTransfo         


        
2条回答
  •  北海茫月
    2021-01-11 16:23

    The XML output will contain a header that is based on the encoding of the stream, not the encoding specified in the settings. As strings are 16 bit unicode the encoding will be UTF-16. The workaround is to suppress the header and add it yourself instead:

    writerSettings.OmitXmlDeclaration = true;
    

    Then when you get the result from the StringBuilder:

    string xml = "\r\n" + sb.ToString();
    

提交回复
热议问题