473,659 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How: Setting UTF8 as an application wide text encoding format

XmlTextReader myXmlReader = new XmlTextReader(a rgs[0]);
string en = myXmlReader.Enc oding.EncodingN ame;
//Console.WriteLi ne(x);

Error:

Unhandled Exception: System.NullRefe renceException: 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 5873
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.Diagnost ics.Debug.Write Line( reader.Encoding .EncodingName );
2) All strings object in the framework are unicode encoded. You can work
with the various encoding classes in System.Text.Enc oding 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.c om> wrote in message
news:0a******** *************** *****@phx.gbl.. .
XmlTextReader myXmlReader = new XmlTextReader(a rgs[0]);
string en = myXmlReader.Enc oding.EncodingN ame;
//Console.WriteLi ne(x);

Error:

Unhandled Exception: System.NullRefe renceException: 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>";
XmlTextReade r reader = new XmlTextReader( new StringReader ( xml ) );//go to the first node
reader.Read( );
System.Diagnos tics.Debug.Writ eLine( reader.Encoding .EncodingName );

2) All strings object in the framework are unicode encoded. You can workwith the various encoding classes in System.Text.Enc oding 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.c om> wrote in message
news:0a******* *************** ******@phx.gbl. ..
XmlTextReader myXmlReader = new XmlTextReader(a rgs[0]);
string en = myXmlReader.Enc oding.EncodingN ame;
//Console.WriteLi ne(x);

Error:

Unhandled Exception: System.NullRefe renceException: 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.c om> 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>";
XmlTextReade r reader = new XmlTextReader( new StringReader

( xml ) );
//go to the first node
reader.Read( );
System.Diagnos tics.Debug.Writ eLine(

reader.Encoding .EncodingName );


2) All strings object in the framework are unicode

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

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.c om> wrote in message
news:0a******* *************** ******@phx.gbl. ..
XmlTextReader myXmlReader = new XmlTextReader(a rgs[0]);
string en = myXmlReader.Enc oding.EncodingN ame;
//Console.WriteLi ne(x);

Error:

Unhandled Exception: System.NullRefe renceException: 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
3739
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 PHP? If I build a content management system (I have) and I make sure that all input is encoded as UTF-8, how will the server know that the text in the MySql database is UTF-8? I'm taking all user input and using this function on the input: ...
5
103572
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" action="processupload.aspx"> <input type=checkbox name="chkOverride" value="1"> <input type=file name="filename"> <input type=submit value="Upload Data File" name="cmdSubmit">
5
17386
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. string get_url = "http://scmvs4:9090/gtccinfo/H485W020.HTML"; --url contains a form string post_url = "http://scmvs4:9090/cgi-bin/gticglnk/P485VEGA"; --called by get_Url upon submit
4
2147
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 Settings\\kst\\BE.dat"; try { FileStream* fs = new FileStream(path, FileMode::Open); StreamReader* sr = new StreamReader(fs);
7
4251
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" value="Merchant Name"> <input type="hidden" name="OrderID" value="Unique OrderID value"> <input type="hidden" name="email" value="Customers email address (OPTIONAL)">
1
4562
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. Text checks for max. length of the field & whether the field is mandatory or optional Example:
2
8414
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 XmlTextWriter(Response.OutputStream, Encoding.UTF8) That fixed the issue. I'm not generating an XML file using a different method grabbing XML from a DB and then reading it in as a stringreader, then transforming that via an
0
5057
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 get the db to translate to UTF-8 for non technical reasons. So I have a string coming back with the character œ (ISO value 156). this character appears in .NET as a box character because 156 is not a valid Unicode character value. I have been...
3
8903
by: sam | last post by:
same as subject?
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8523
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.