TextWriter encoding

C

coder316

Hello,
I'm 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'm getting an error

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

How do I change this?
Thanks
 
C

coder316

Hello,
I'm 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'm getting an error

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

How do I change this?
Thanks

I tried this too:

TextWriter w = new StringWriter();

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

mick

coder316 said:
Hello,
I'm 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'm getting an error

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

I think you set up the encoding when you new up XmlTextWriter. Have a look
at the overloads for it.

mick
 
M

mick

Hello,
I'm 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'm getting an error

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

How do I change this?
Thanks

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
 
J

Jeff Johnson

I'm 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'm getting an error

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

How do I change this?

You appear to be writing to a string (since you're 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're 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.

===============
 
P

Peter Duniho

coder316 said:
Hello,
I'm 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'm getting an error

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

How do I change this?

You don't 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
 

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