Write utf8 encoded string

S

Samuel

Hi

I am trying to write to a string text encoded to utf8 as oppose to utf16

Since the data comes from an XML object (and I serialize it) I need to pass
either StreamWriter or a StringWriter object, I don't want to create a file
so I want to use a StringWriter (passing to it's constructor a
StringBuilder) The problem is that the StringWriter encodes utf16 (I don't
know how to change it)

Any Advice?

Thank you in advance,
Samuel
 
G

Göran Andersson

Samuel said:
I am trying to write to a string text encoded to utf8 as oppose to utf16

Since the data comes from an XML object (and I serialize it) I need to pass
either StreamWriter or a StringWriter object, I don't want to create a file
so I want to use a StringWriter (passing to it's constructor a
StringBuilder) The problem is that the StringWriter encodes utf16 (I don't
know how to change it)

No, a StringWriter doesn't encode anything at all. What you write to it
ends up in the internal StringBuilder as characters, not encoded as bytes.

Why do you think that the StringWriter would encode the text? How are
you using it?
 
H

Herfried K. Wagner [MVP]

Samuel said:
I am trying to write to a string text encoded to utf8 as oppose to utf16

Since the data comes from an XML object (and I serialize it) I need to
pass either StreamWriter or a StringWriter object, I don't want to create
a file so I want to use a StringWriter (passing to it's constructor a
StringBuilder) The problem is that the StringWriter encodes utf16 (I don't
know how to change it)

Strings in .NET are always encoded in UTF-16 when stored in memory. You
cannot change that. If you want to get the byte representation of a string
in a certain encoding, check 'System.Text.Encoding.GetBytes'.
 
S

Samuel

Let's start from the beginning

I have an object which I am trying to covert to XML string I later take the
string and send the Bytes content to another server using a webservice

In order to do that I first decalre this:
System.Xml.Serialization.XmlSerializer

I use the above object's Serialize method to covert the data object to
string

I need to pass a stream so the XmlSerializer will send the data there

When I use a file stream (StreamWriter) I can specify the Encoding type (New
System.Text.UTF8Encoding)

However when I use a StringWriter I can't

I assume that the XmlSerializer encodes based on the stream type

And here is the main thing: When I use the FileStream I get an XML file
starting with <?xml version="1.0" encoding="utf-8"?> and when I use the
StringWriter I get <?xml version="1.0" encoding="utf-16"?>

THIS MAY NOT BE THE ACTUAL ENCODING but when I upload to a server that may
require utf8 then it will confuse it if it is preceeded with 16


Thank you,
Samuel
 
C

Cor Ligthert[MVP]

THIS MAY NOT BE THE ACTUAL ENCODING

Herfried wrote:
STRINGS IN .NET ARE ALWAYS ENCODED IN UTF-16 WHEN STORED IN MEMORY
 
G

Göran Andersson

Samuel said:
Let's start from the beginning

That's one way of doing it. ;)
I have an object which I am trying to covert to XML string I later take the
string and send the Bytes content to another server using a webservice

In order to do that I first decalre this:
System.Xml.Serialization.XmlSerializer

I use the above object's Serialize method to covert the data object to
string

I need to pass a stream so the XmlSerializer will send the data there

When I use a file stream (StreamWriter) I can specify the Encoding type (New
System.Text.UTF8Encoding)

However when I use a StringWriter I can't

I assume that the XmlSerializer encodes based on the stream type

And here is the main thing: When I use the FileStream I get an XML file
starting with <?xml version="1.0" encoding="utf-8"?> and when I use the
StringWriter I get <?xml version="1.0" encoding="utf-16"?>

THIS MAY NOT BE THE ACTUAL ENCODING but when I upload to a server that may
require utf8 then it will confuse it if it is preceeded with 16


Thank you,
Samuel

The StringWriter has an Encoding property that you can use for this. It
doesn't change how the write works, but it provides information about
the encoding when the XML is created.
 

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