473,804 Members | 3,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error: There is no Unicode byte order mark. Cannot switch to Unicode

I have a client server set of apps that can connect through socets and send
data back and forth. I'm trying to get it to send XML messages back and
both. Currently it works as string data. I collect all of the incoming data
to a string but when I try to parse the incoming XML I get the following
message:

-------------------------------------------
Error Parsing message: System.Xml.Exce ption: There is no Unicode byte order
mark. Cannot switch to unicode.
-------------------------------------------

I use XmlTextWriter and build it into a StringWriter. The StringWriter
encodes into UTF-16 instead of UTF-8, so I'm not if this is the problem. I
convert the StringWriter into a string which I then send across the wire to
the server. The server recieves the data with no problem and use a
StringBuilder to collect it. I conver the StringBuilder to a string. The
string is given to a parseMessage() class which encodes
(System.Text.En coding.ASCII.Ge tBytes) the string into a Stream. The Stream
is then read in the XmlTextReader where it throws the exception.

// *********** CLASS TO WRITE XML **************
public string writePeerAdv(st ring type, string command)
{
// StringWriter to write to
StringWriter sw = new StringWriter();

// Create XmlTextWriter object
XmlTextWriter writer = new XmlTextWriter(s w);

// Begin Document
writer.WriteSta rtDocument();
writer.Formatti ng = Formatting.Inde nted;
// Write the file
writer.WriteSta rtElement("Mess age");
writer.WriteEle mentString("Typ e", type);
writer.WriteEle mentString("Com mand", command);
writer.WriteEnd Element();
// Close Document
writer.Close();

// Return string
string peerAdv = sw.ToString();
return peerAdv;
}
// *************** *************** *************** ***
// ******* CLASS TO PARSE XML *************** ****
public void parseMessage(st ring incomingMessage )
{
// Create Strem to read from
Stream stream = new
MemoryStream(Sy stem.Text.Encod ing.ASCII.GetBy tes(incomingMes sage));

// Read the stream into XmlTextReader
XmlTextReader reader = new XmlTextReader(s tream);
while (reader.Read())
{
if (reader.NodeTyp e == XmlNodeType.Ele ment)
{
MessageBox.Show ("Found an Element: " + reader.Value.To String());
}
}
// Close the stream
reader.Close();
}
// *************** *************** *************** ***

Any ideas? Thanks for the help,
Jim
Nov 12 '05 #1
1 27249
There's no need to use GetBytes. In fact, that may cause you some problems
if non-ASCII information comes with the string, which is always Unicode in
..NET.
You should use a StringReader instead, and pass it directly to the
XmlTextReader ctor:

XmlTextReader tr = new XmlTextReader(n ew StringReader( incomingMessage ) );
HTH

--
Daniel Cazzulino
Lagash Systems SA
http://weblogs.asp.net/cazzu

"Jim P." <tr**********@y ahoo.com> wrote in message
news:uq******** ******@TK2MSFTN GP12.phx.gbl...
I have a client server set of apps that can connect through socets and send data back and forth. I'm trying to get it to send XML messages back and
both. Currently it works as string data. I collect all of the incoming data to a string but when I try to parse the incoming XML I get the following
message:

-------------------------------------------
Error Parsing message: System.Xml.Exce ption: There is no Unicode byte order mark. Cannot switch to unicode.
-------------------------------------------

I use XmlTextWriter and build it into a StringWriter. The StringWriter
encodes into UTF-16 instead of UTF-8, so I'm not if this is the problem. I convert the StringWriter into a string which I then send across the wire to the server. The server recieves the data with no problem and use a
StringBuilder to collect it. I conver the StringBuilder to a string. The
string is given to a parseMessage() class which encodes
(System.Text.En coding.ASCII.Ge tBytes) the string into a Stream. The Stream is then read in the XmlTextReader where it throws the exception.

// *********** CLASS TO WRITE XML **************
public string writePeerAdv(st ring type, string command)
{
// StringWriter to write to
StringWriter sw = new StringWriter();

// Create XmlTextWriter object
XmlTextWriter writer = new XmlTextWriter(s w); x // Begin Document
writer.WriteSta rtDocument();
writer.Formatti ng = Formatting.Inde nted;
// Write the file
writer.WriteSta rtElement("Mess age");
writer.WriteEle mentString("Typ e", type);
writer.WriteEle mentString("Com mand", command);
writer.WriteEnd Element();
// Close Document
writer.Close();

// Return string
string peerAdv = sw.ToString();
return peerAdv;
}
// *************** *************** *************** ***
// ******* CLASS TO PARSE XML *************** ****
public void parseMessage(st ring incomingMessage )
{
// Create Strem to read from
Stream stream = new
MemoryStream(Sy stem.Text.Encod ing.ASCII.GetBy tes(incomingMes sage));

// Read the stream into XmlTextReader
XmlTextReader reader = new XmlTextReader(s tream);
while (reader.Read())
{
if (reader.NodeTyp e == XmlNodeType.Ele ment)
{
MessageBox.Show ("Found an Element: " + reader.Value.To String());
}
}
// Close the stream
reader.Close();
}
// *************** *************** *************** ***

Any ideas? Thanks for the help,
Jim

Nov 12 '05 #2

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

Similar topics

8
3668
by: Francis Girard | last post by:
Hi, For the first time in my programmer life, I have to take care of character encoding. I have a question about the BOM marks. If I understand well, into the UTF-8 unicode binary representation, some systems add at the beginning of the file a BOM mark (Windows?), some don't. (Linux?). Therefore, the exact same text encoded in the same UTF-8 will result in two different binary files, and of a slightly different length. Right ?
6
18336
by: Spamtrap | last post by:
I only work in Perl occasionaly, and have been searching for a solution for a conversion, and everything I found seems much too complex. All I need to do is take a simple text file and copy it, however some specific lines are in fact in UTF8 as printed garbagy characters and they need to be converted to Unicode, so that the new text file can be imported into a desktop program and into some Word documents. For the moment I would be...
3
3328
by: Naresh Agarwal | last post by:
Hi XML uses UTF-8 by default. Is that correct? Also, can we use Unicode in XML? thanks, Naresh
4
2466
by: Majed | last post by:
Hi , all I'm trying to write unicode to a file for another app (not developed with vs2003) to read it. I used StreamWriter with unicode encoding.but I was surprised that the streamwriter adds FFFE to the start of the file,which stopes the other app from reading it!! any idea how to stope it frome doing that,do I have to use another class #####writer that supports unicode? help me Please! Thanks
8
4072
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had problems getting my algorithm to work and in order to help me find the solution I decided to print each line to screen as I read them. Then, to my surprise, I noticed that there was a space between every character as I outputted the lines to the...
5
3338
by: srikant | last post by:
I am writing a client in C# that needs to communicate over the network to a legacy C++ application that uses Unicode strings. I realize that C# strings are already in Unicode, however, how do I account for the network order transformation. Can I simply do the equivalent of string = "hello world"; // this is a unicode strings (2 bytes per char) bytes buffer = UnicodeEncoding.GetBytes(str); myNetworkStream.Write(buffer, 0, buffer.Length); ...
1
1779
by: Scott Duckworth | last post by:
Can anyone provide a quick code snippit to open a text file and tell if it's ASCII or Unicode? Thanks
18
620
by: Chameleon | last post by:
I am trying to #define this: #ifdef UNICODE_STRINGS #define UC16 L typedef wstring String; #else #define UC16 typedef string String; #endif ....
1
7259
by: alpacabrian | last post by:
I've been working on a Virtual Earth mapping browser application and ran into a problem where the app is popping up a message containing: There is an error in xml document(0, 0). - Thre is no Unicode byte order mark. I'm not sure where it's coming from. A Google search on the error seems to give only details on writing XML files, which I'm not doing, not intentionally at least. I suspect its not the app I'm using but something in my...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10577
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...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
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
10077
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
9150
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...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
3
2991
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.