473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How: Setting UTF8 as an application wide text encoding format

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
3 5849
xmlguy,

1) What's args[0] in your example? Does it contain any Xml? In that case you
first have to pass that sting off to a StringReader. If it's a path to a
file then it's OK.

You have to actually start reading the contents of the XML document. You do
this by calling the Read() method for the first time. This sample code works
for me:

string xml = "<foo>bar</foo>";
XmlTextReader reader = new XmlTextReader( new StringReader( xml ) );
//go to the first node
reader.Read();
System.Diagnostics.Debug.WriteLine( reader.Encoding.EncodingName );
2) All strings object in the framework are unicode encoded. You can work
with the various encoding classes in System.Text.Encoding to create byte[]s
and MemoryStreams that contain text encoded in a different encoding scheme.
You cannot change the encoding of string objects.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"xmlguy" <xm****@yahoo.com> wrote in message
news:0a****************************@phx.gbl...
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 #2
I guess I should have specified.

args[0] on the commandline is the name of a .xml file
containing XML with UTF-8 as its encoding.

That is the wierd thing as to why everything defaults to
UTF-16 ?
-----Original Message-----
xmlguy,

1) What's args[0] in your example? Does it contain any Xml? In that case youfirst have to pass that sting off to a StringReader. If it's a path to afile then it's OK.

You have to actually start reading the contents of the XML document. You dothis by calling the Read() method for the first time. This sample code worksfor me:

string xml = "<foo>bar</foo>";
XmlTextReader reader = new XmlTextReader( new StringReader ( xml ) );//go to the first node
reader.Read();
System.Diagnostics.Debug.WriteLine( reader.Encoding.EncodingName );

2) All strings object in the framework are unicode encoded. You can workwith the various encoding classes in System.Text.Encoding to create byte[]sand MemoryStreams that contain text encoded in a different encoding scheme.You cannot change the encoding of string objects.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"xmlguy" <xm****@yahoo.com> wrote in message
news:0a****************************@phx.gbl...
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 #3
The encoding of the data in your file becomes irrelevant once you use a
StreamReader to read the file content into a strings. Strings are always
UTF-16 in .NET. You have to work with byte[] instead of strings if you need
to preserve the character encoding.

Does it matter what format the data is encoded in within your application?
Or do you simply need to be able to write it back to a file in UTF-8
encodeing?
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"xmlguy" <xm****@yahoo.com> wrote in message
news:04****************************@phx.gbl...
I guess I should have specified.

args[0] on the commandline is the name of a .xml file
containing XML with UTF-8 as its encoding.

That is the wierd thing as to why everything defaults to
UTF-16 ?
-----Original Message-----
xmlguy,

1) What's args[0] in your example? Does it contain any

Xml? In that case you
first have to pass that sting off to a StringReader. If

it's a path to a
file then it's OK.

You have to actually start reading the contents of the

XML document. You do
this by calling the Read() method for the first time.

This sample code works
for me:

string xml = "<foo>bar</foo>";
XmlTextReader reader = new XmlTextReader( new StringReader

( xml ) );
//go to the first node
reader.Read();
System.Diagnostics.Debug.WriteLine(

reader.Encoding.EncodingName );


2) All strings object in the framework are unicode

encoded. You can work
with the various encoding classes in System.Text.Encoding

to create byte[]s
and MemoryStreams that contain text encoded in a

different encoding scheme.
You cannot change the encoding of string objects.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"xmlguy" <xm****@yahoo.com> wrote in message
news:0a****************************@phx.gbl...
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 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lawrence | last post by:
How do I get PHP to tell the server that when I echo text to the screen, I need for the text to be sent as UTF-8? How does Apache know the right encoding when all the text is being generated by...
5
by: John Lee | last post by:
Hi, I have a simple web page that allow file to be uploaded, the upload page looks like the following: <form method="post" name="upload" enctype="multipart/form-data"...
5
by: Tammy | last post by:
Hi, I have an aspx app which needs to post data to a form and read the response. I am confused on whether I should be using the get_url using "POST" method or the post_url using "GET" method. ...
4
by: Kueishiong Tu | last post by:
I have a text file with wide characters. I use the following C++ code to read them in. However the wide characters are not read in properly. What is wrong? String* path = "C:\\Documents and...
7
by: | last post by:
Hello, I would like to do the following from a asp.net button click: <form method="POST" action="https://www.1234.com/trans_center/gateway/direct.cgi"> <input type="hidden" name="Merchant"...
1
by: sonald | last post by:
Dear All, I am working on a module that validates the provided CSV data in a text format, which must be in a predefined format. We check for the : 1. Number of fields provided in the text file,...
2
by: darrel | last post by:
I was having issues before with my XML being sent to the browser as UTF16. Via my XmlTextWriter, was able to fix this by explicitely setting the encoding to UTF8: Dim objX As New...
0
by: deloford | last post by:
Hi This is going to be a question for anyone who is an expert in C# Text Encoding. My situation is this: I have a Sybase database which is firing back ISO-8559 encoded strings. I am unable to...
3
by: sam | last post by:
same as subject?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.