Home All Groups Group Topic Archive Search About

XmlWriter do not use settings specified in XmlWriter.Create

Author
28 Sep 2006 8:55 AM
av
hi all,
please review the following code snipped, my goal is just to transform a
document.
The problem is that XmlWriter.Create do not honor passed
Trans.OutputSettings. The debuggin shows that Trans.OutputSettings.Encoding
is set to {System.Text.UTF8Encoding}, but OutWriter.Settings.Encoding is
{System.Text.UnicodeEncoding} after the call to Create().
Can you explain it and provide a workaround?

            XmlDocument docXsl = new XmlDocument();
            docXsl.LoadXml(transform);

            XslCompiledTransform Trans = new XslCompiledTransform();
            Trans.Load(docXsl);
            StringBuilder Out = new StringBuilder();

            XmlWriter OutWriter = XmlWriter.Create(Out, Trans.OutputSettings
);
            Trans.Transform(doc, OutWriter);

here are the beginnings of the xml and stylesheet invloved:

doc:
<?xml version ="1.0" encoding="utf-8"?>


docXsl:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output method="html" omit-xml-declaration="yes" encoding="UTF-8" />

Author
28 Sep 2006 9:18 AM
Joerg Jooss
Thus wrote AV,

Show quote
> hi all,
> please review the following code snipped, my goal is just to transform
> a
> document.
> The problem is that XmlWriter.Create do not honor passed
> Trans.OutputSettings. The debuggin shows that
> Trans.OutputSettings.Encoding
> is set to {System.Text.UTF8Encoding}, but OutWriter.Settings.Encoding
> is
> {System.Text.UnicodeEncoding} after the call to Create().
> Can you explain it and provide a workaround?
> XmlDocument docXsl = new XmlDocument();
> docXsl.LoadXml(transform);
> XslCompiledTransform Trans = new XslCompiledTransform();
> Trans.Load(docXsl);
> StringBuilder Out = new StringBuilder();
> XmlWriter OutWriter = XmlWriter.Create(Out,
> Trans.OutputSettings
> );
> Trans.Transform(doc, OutWriter);
[...]

A .NET string is always UTF-16 encoded -- there's really no such thing as
an UTF-8 string in .NET.

You can use a MemoryStream to create an in-memory representation that is
encoded with UTF-8 (or any other encoding).

Cheers,
--
Joerg Jooss
news-re***@joergjooss.de

AddThis Social Bookmark Button