Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
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...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
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...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
    
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...
 
Email Archiving and Email Filing - what’s the difference?
Web-based task/todo list management
 
    
Steven Livingstone
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...
 
Open source windows
The Law Society’s guidelines on e-mail management
 
    
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




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734