XmlWriter do not use settings specified in XmlWriter.Create

G

Guest

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:blush:utput method="html" omit-xml-declaration="yes" encoding="UTF-8" />
 
J

Joerg Jooss

Thus wrote 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);
[...]

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top