Topaz Filer - Email filing software
who controls the encoding when transforming xml?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.xml.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Jiho Han
I do a transform of an xml document into another xml using XslTransform.
In my xsl file, I specify using <xsl:output encoding="utf-8"/>. However,
when my transform is done, the resulting xml is in utf-16.
What gives? Anyone?

Reply to this message...
Vote that this is a GOOD answer...
 
New BootFX DBGet build available
Twitter and Snow… simple #uksnow
 
    
Steven Livingstone
Your Xml doc is probably UTF-16 encoded. You might want to check what
encoding the Xml doc was saved with. Maybe even try saving it as UTF-8 - in
notepad say.

Any joy?

Steven
Founder, http://venturetogether.com

"Jiho Han" <Click here to reveal e-mail address> wrote in message
news:#Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Windows 7 compatible tool for mounting ISO images
Introduction to BootFX’s Object Relational Mapping Functionality article now live on CodeProject
 
    
Oleg Tkachenko [MVP] (VIP)
Jiho Han wrote:

[Original message clipped]

Show us your code. <xsl:output encoding="utf-8"/> only works when you
transform to Stream. Otherwise it's ignored and other stuff defines
output encoding.
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Jiho Han
This is my xsl header section:

<?xml version="1.0" ?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output encoding="utf-8" indent="no" media-type="text/xml"
method="xml"/>

This is my data:

<association>

<associationid>5b5acf84-6a76-4447-ad88-dc0b7d031741</associationid>

</association>

Here's my code for the transform:

XmlDocument xdoc = new XmlDocument();

xdoc.LoadXml(dataXml);

XmlDeclaration xmldecl;

xmldecl = xdoc.CreateXmlDeclaration("1.0", null, null);

xmldecl.Encoding="UTF-8";

xdoc.InsertBefore(xmldecl, xdoc.DocumentElement);

XslTransform xslt = new XslTransform();

xslt.Load(Server.MapPath("client/association.xsl"));

StringBuilder sb = new StringBuilder();

StringWriter sw = new StringWriter(sb);

xslt.Transform(xdoc, null, sw);

return sb.ToString();

As you can see I tried to insert a XML declaration in there but it really
didn't do much.
Thanks for looking into this. Much appreciated.
Jiho

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:%Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Xenu Link Sleuth
Chromium OS – really?
 
    
Steven Livingstone
GOOD ANSWER
Instead of using StringWriter, try using a Stream such as follows :

MemoryStream ms = new MemoryStream();
xslt.Transform(xdoc, null, ms);

ms.Position = 0;
StreamReader sr = new StreamReader(ms, Encoding.UTF8);

return(sr.ReadToEnd());

This wiill output a UTF-8 encoded string.

Steven, XmlInsider
http://stevenlivingstone.com

"Jiho Han" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Jiho Han
Thanks guys. You were very helpful.

"Steven Livingstone" <Click here to reveal e-mail address> wrote in message
news:%Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Oleg Tkachenko [MVP] (VIP)
Jiho Han wrote:

[Original message clipped]

Encoding of source XML document actually has noting to do with output
encoding.

[Original message clipped]

As stated here -
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconinputsoutputstoxsltransform.asp?frame=true
when transforming to TextWriter (StringWriter is TextWriter), encoding
attribute of xsl:encoding element is ignored and instead encoding of the
TExtWriter is used. Strings are always UTF-16 encoded hence with
StringWriter the result is alwaus UTF-16 encoded.
If you want to control output encoding from within XSLT stylesheet,
transform to Stream, such as MemoryStream.
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Reply to this message...
Vote that this is a GOOD answer...
 
 
 
System.IO.MemoryStream
System.IO.StreamReader
System.IO.StringWriter
System.IO.TextWriter
System.Text.Encoding
System.Text.StringBuilder
System.Xml.XmlDeclaration
System.Xml.XmlDocument
System.Xml.Xsl.XslTransform




Topaz Filer - Email filing software
Ad
BootFX
Reliable and powerful .NET application framework.
Looking to invest in a major software project? Technical and commercial advice available here.
Other Helpful Sites
MBR 247
Topaz Filer
SharePoint Email Filing
Software Advisory Services
 
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734