Mystery .NET XslTransform - META character set resulting from conversion

I have the following code:

        using (Stream stream = new MemoryStream())
        {
            xslt.Transform(document, xslArg, stream);
            stream.Seek(0, SeekOrigin.Begin);
            StreamReader reader = new StreamReader(stream);
            var result = reader.ReadToEnd();
            return result;
        }

This conversion outputs an HTML document. What puzzles me is that although the xsl input contains:

        <html>
            <head>
                <style>
                    @page Section1
                    {size:612.0pt 792.0pt;
                    margin:42.55pt 42.55pt 42.55pt 70.9pt;
                    mso-header-margin:35.45pt;
                    mso-footer-margin:35.45pt;
                    mso-paper-source:0;}
                    div.Section1
                    {page:Section1;}
            </head>
            <body>
                <div class="Section1">
                .....

:

<html xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style>.....

as you can see, charset info has been added, among other things.

But what really struck me was that when I changed the code that translates to:

    StringBuilder sb = new StringBuilder();
    using (StringWriter writer = new StringWriter(sb))
    {
        xslt.Transform(document, xslArg, writer);
    }
    var result = sb.ToString();
    return result;

the generated output was as follows:

<html xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-16">
    <style>....

As you can see, the encoding has changed. I think this is because StringBuilder and .NET work with UTF-16 by default. But why does the conversion add an encoding META tag anyway?

+3
source share
1 answer

, <xsl:output method="html"/>, html . XSLT , XSLT- - ​​ .

+4

All Articles