.NET Framework - TextWriter encoding

Asked By coder316
18-Dec-09 10:07 PM
Hello,
I am trying to change the encoding for a textwriter:
TextWriter w = new StringWriter();

XmlTextWriter xmlWriter = new XmlTextWriter(w);
w.Encoding = System.Text.Encoding.Unicode;

But I am getting an error

Property or indexer 'System.IO.TextWriter.Encoding' cannot be assigned
to -- it is read only

How do I change this?
Thanks
System.Text.Encoding.Unicode
(1)
XmlWriter.Create
(1)
StringWriter
(1)
TextWriter
(1)
Encoding.Unicode
(1)
Encoding
(1)
Strings
(1)
XmlWriterSettings
(1)
  coder316 replied to coder316
18-Dec-09 10:07 PM
I tried this too:

TextWriter w =3D new StringWriter();

XmlTextWriter xmlWriter =3D new XmlTextWriter(w);
Encoding unicode =3D Encoding.Unicode;
w.Encoding =3D unicode;
  mick replied to coder316
18-Dec-09 12:57 PM
I think you set up the encoding when you new up XmlTextWriter. Have a look
at the overloads for it.

mick
  mick replied to coder316
18-Dec-09 01:00 PM
I tried this too:

TextWriter w = new StringWriter();

XmlTextWriter xmlWriter = new XmlTextWriter(w);
Encoding unicode = Encoding.Unicode;
w.Encoding = unicode;

If Encoding is read-only you wont bet able to set it to anything. I imagine
it just tells you what encoding has been set, although thats just a guess as
Ive never used it myself.

mick
  Jeff Johnson replied to coder316
18-Dec-09 01:07 PM
You appear to be writing to a string (since you are using a StringWriter),
and strings are ALWAYS Unicode. The only time an encoding would come into
play would be when writing the string to a stream.

Also, by creating an XmlTextWriter directly, you are doing things "the old
way." From MSDN:

===============
In the .NET Framework version 2.0 release, the recommended practice is to
create XmlWriter instances using the XmlWriter.Create method and the
XmlWriterSettings class. This allows you to take full advantage of all the
new features introduced in this release. For more information, see Creating
XML Writers.

===============
  Peter Duniho replied to coder316
18-Dec-09 01:38 PM
You do not change it.  You provide the desired encoding when creating the
XmlTextWriter in the first place (by using a constructor that takes as
an argument the encoding, or by passing a TextWriter with the
appropriate encoding already set).

That said, in this case you are writing to a StringWriter, so you have
no choice of the encoding.  Strings are always UTF-16, so the encoding
always winds up being UTF-16, no matter what encoding you tell
XmlTextWriter to use.

Pete
Create New Account
help
sEmailZender) message.Subject = "RIE report for site " & rc.sSitenummer message.Body = strbody.ToString message.BodyEncoding = System.Text.Encoding.Unicode message.IsBodyHtml = True message.Attachments.Add(data1) data1 = Nothing 'attach PDF file to message Dim 1) System.IO.File.GetLastWriteTime (1) System.IO.File.GetCreationTime (1) MediaTypeNames.Application.Octet (1) System.Text.Encoding.Unicode (1) ClientScript.RegisterStartupScript (1) SmtpDeliveryMethod.Network (1) SmtpDeliveryMethod (1) You may be doing everything just
09:47, schrieb jainhemant007: In a VB group, you get a VB answer. :-) dim bytes = system.text.encoding.unicode.getbytes(filename) - - Armin keywords: want, to, convert, string, into, byte, array, hw, i, can, do
sEmailZender) message.Subject = "RIE report for site " & rc.sSitenummer message.Body = strbody.ToString message.BodyEncoding = System.Text.Encoding.Unicode message.IsBodyHtml = True message.Attachments.Add(data1) data1 = Nothing 'attach PDF file to message Dim
System::String ^G = String::Empty; / / G = Get_Hash(System::Text::Encoding::ASCII-> GetBytes("Geheim")); / / G = Get_Hash(System::Text::Encoding::Unicode-> GetBytes("Geheim")); G = Get_Hash(System::Text::Encoding::Default-> GetBytes("Geheim")); bring mir aber einen anderen
gesammelte Datenbankeintr?ge (item) wie folgt durchlaufen: using (StreamWriter sw = new StreamWriter("Products.txt", false, System.Text.Encoding.Unicode)) { / / Obtain the persistent object class info required by the GetObjects method customClass = uow.GetClassInfo(typeof