470,641 Members | 2,653 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

error getting the encoding name from an XmlTextReader

XmlTextReader myXmlReader = new XmlTextReader(args[0]);
string en = myXmlReader.Encoding.EncodingName;
//Console.WriteLine(x);

Error:

Unhandled Exception: System.NullReferenceException: Object
reference not set to
an instance of an object.

HOW CAN I GET THE ENCODING NAME ? Basically determine the
encoding type.

Also How can I "set" an application wide Encoding ? I want
all modules and everything to stick to and conform to UTF-
8.

But somehow some methods automatically go to UTF-16
Nov 11 '05 #1
4 3286
xmlguy wrote:
XmlTextReader myXmlReader = new XmlTextReader(args[0]);
string en = myXmlReader.Encoding.EncodingName;
//Console.WriteLine(x);

Error:

Unhandled Exception: System.NullReferenceException: Object
reference not set to
an instance of an object.

HOW CAN I GET THE ENCODING NAME ? Basically determine the
encoding type.
You have to call Read() method at least once if you want to get Encoding
property. That's because XmlReader is pull-mode parser - it's your
responsibbility to call its Read() method to force it to read XML.
Encoding is stored in XML declarartion, which is always the very first
line in XML, so single Read() call would be enough to read it.
Also How can I "set" an application wide Encoding ? I want
all modules and everything to stick to and conform to UTF-
8.

Elaborate please, what do you mean.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2
Is there any way to specify that all modules or
fns/methods in a given application follow only a single
encoding e.g. UTF-8

Because somehow I input my XML as UTF-8 and it
automatically comes out ast UTF-16.

How can i ensure application-wide setting/configuration
that it stick to one format.
-----Original Message-----
xmlguy wrote:
XmlTextReader myXmlReader = new XmlTextReader(args[0]);
string en = myXmlReader.Encoding.EncodingName;
//Console.WriteLine(x);

Error:

Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.

HOW CAN I GET THE ENCODING NAME ? Basically determine the encoding type.
You have to call Read() method at least once if you want

to get Encodingproperty. That's because XmlReader is pull-mode parser - it's yourresponsibbility to call its Read() method to force it to read XML.Encoding is stored in XML declarartion, which is always the very firstline in XML, so single Read() call would be enough to read it.
Also How can I "set" an application wide Encoding ? I want all modules and everything to stick to and conform to UTF- 8.

Elaborate please, what do you mean.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

.

Nov 11 '05 #3
xmlguy wrote:
Is there any way to specify that all modules or
fns/methods in a given application follow only a single
encoding e.g. UTF-8

Because somehow I input my XML as UTF-8 and it
automatically comes out ast UTF-16.

How can i ensure application-wide setting/configuration
that it stick to one format.


I believe that's unfeasible and as well as quite unusual, so there is no
such functionality in .NET. Some situations doesn't allow UTF-8
altogether, e.g. strings are always UTF-16 encoded.
You have to take care of encoding issues on yours own. Beware of
implicit recodings.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #4
Here's an interesting article discussing Unicode and character sets

http://www.joelonsoftware.com/articles/Unicode.html

Eirik M.

"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
xmlguy wrote:
Is there any way to specify that all modules or
fns/methods in a given application follow only a single
encoding e.g. UTF-8

Because somehow I input my XML as UTF-8 and it
automatically comes out ast UTF-16.

How can i ensure application-wide setting/configuration
that it stick to one format.


I believe that's unfeasible and as well as quite unusual, so there is no
such functionality in .NET. Some situations doesn't allow UTF-8
altogether, e.g. strings are always UTF-16 encoded.
You have to take care of encoding issues on yours own. Beware of
implicit recodings.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bill Cohagan | last post: by
reply views Thread by Sangeetha Nagaraj | last post: by
reply views Thread by SqlJunkies User | last post: by
1 post views Thread by stuart dent via .NET 247 | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.