By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,473 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.