473,394 Members | 1,642 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,394 software developers and data experts.

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.Exception: 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.Encoding.ASCII.GetBytes) 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(string type, string command)
{
// StringWriter to write to
StringWriter sw = new StringWriter();

// Create XmlTextWriter object
XmlTextWriter writer = new XmlTextWriter(sw);

// Begin Document
writer.WriteStartDocument();
writer.Formatting = Formatting.Indented;
// Write the file
writer.WriteStartElement("Message");
writer.WriteElementString("Type", type);
writer.WriteElementString("Command", command);
writer.WriteEndElement();
// Close Document
writer.Close();

// Return string
string peerAdv = sw.ToString();
return peerAdv;
}
// ************************************************
// ******* CLASS TO PARSE XML *******************
public void parseMessage(string incomingMessage)
{
// Create Strem to read from
Stream stream = new
MemoryStream(System.Text.Encoding.ASCII.GetBytes(i ncomingMessage));

// Read the stream into XmlTextReader
XmlTextReader reader = new XmlTextReader(stream);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
MessageBox.Show("Found an Element: " + reader.Value.ToString());
}
}
// Close the stream
reader.Close();
}
// ************************************************

Any ideas? Thanks for the help,
Jim
Nov 12 '05 #1
1 27160
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(new StringReader( incomingMessage ) );
HTH

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

"Jim P." <tr**********@yahoo.com> wrote in message
news:uq**************@TK2MSFTNGP12.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.Exception: 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.Encoding.ASCII.GetBytes) 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(string type, string command)
{
// StringWriter to write to
StringWriter sw = new StringWriter();

// Create XmlTextWriter object
XmlTextWriter writer = new XmlTextWriter(sw); x // Begin Document
writer.WriteStartDocument();
writer.Formatting = Formatting.Indented;
// Write the file
writer.WriteStartElement("Message");
writer.WriteElementString("Type", type);
writer.WriteElementString("Command", command);
writer.WriteEndElement();
// Close Document
writer.Close();

// Return string
string peerAdv = sw.ToString();
return peerAdv;
}
// ************************************************
// ******* CLASS TO PARSE XML *******************
public void parseMessage(string incomingMessage)
{
// Create Strem to read from
Stream stream = new
MemoryStream(System.Text.Encoding.ASCII.GetBytes(i ncomingMessage));

// Read the stream into XmlTextReader
XmlTextReader reader = new XmlTextReader(stream);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
MessageBox.Show("Found an Element: " + reader.Value.ToString());
}
}
// 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
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...
6
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...
3
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
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...
8
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...
5
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...
1
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
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
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
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...

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.