473,399 Members | 2,146 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,399 software developers and data experts.

Sending serialized XML by TCP

I want to be able to serialise a .Net object and send the resulting XML by
TCP. How can I extract the serialised XML as a string?
Nov 12 '05 #1
4 6180
Hi,

You can serialize the objects to a memory stream and then send the bytes in
the memory stream buffer over the TCP connection. Assuming you are using
XmlSerializer the following snippet should get you going.

MemoryStream stm = new MemoryStream();
StreamWriter wr = new StreamWriter(stm, System.Text.Encoding.UTF8);
XmlSerializer xs = new XmlSerializer(typeof(DataObject));
xs.Serialize(wr, data);

now the the serialized data can be accessed via the stm.GetBuffer(), the
length of the data contained in the buffer can be accessed via the
stm.Length property.

string str = System.Text.Encoding.UTF8.GetString(stm.GetBuffer( ),0,
(int)stm.Length);

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"BrentonMCA" <Br********@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
I want to be able to serialise a .Net object and send the resulting XML by
TCP. How can I extract the serialised XML as a string?

Nov 12 '05 #2
Chris,

Thank you. It appears to do the job. Just one other query - I am a bit
confused about the first character returned.

When I move the mouse cursor over the variable that contains the required
text, the first character is displayed as unprintable and is followed by
"<?xml..."

When I display the value of the variable in the Command window, the first
character is displayed as a dot, much as I would expect to see chr(250). But
asc(textString.substring(0,1)) prduces 63 (= asc("?")).

When I read a hex dump of the string, the first character is interpreted as
3F (="?").

I guess I can just ignore the first character, but I am curious about these
inconsistencies.

Can you explain them?

Brenton

"Chris Taylor" wrote:
Hi,

You can serialize the objects to a memory stream and then send the bytes in
the memory stream buffer over the TCP connection. Assuming you are using
XmlSerializer the following snippet should get you going.

MemoryStream stm = new MemoryStream();
StreamWriter wr = new StreamWriter(stm, System.Text.Encoding.UTF8);
XmlSerializer xs = new XmlSerializer(typeof(DataObject));
xs.Serialize(wr, data);

now the the serialized data can be accessed via the stm.GetBuffer(), the
length of the data contained in the buffer can be accessed via the
stm.Length property.

string str = System.Text.Encoding.UTF8.GetString(stm.GetBuffer( ),0,
(int)stm.Length);

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"BrentonMCA" <Br********@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
I want to be able to serialise a .Net object and send the resulting XML by
TCP. How can I extract the serialised XML as a string?


Nov 12 '05 #3
Hi,

What you are seeing is the unicode preamble, this is a byte order marker
prepended to unicode files so that an editor can identify the file as
unicode including the unicode transformation and the byte order. To see this
open a text file in notepad and save it in a unicode format and open the
file with a binary editor and you will notice this preamble.

The preamble for UTF-8 is 0xEF, 0xBB, 0XBF, for Unicode it is 0xFF, 0xFE. To
exclude this preamble from the encoding, create a new instance of the
UTFTEncoding and use that.

UTF8Encoding UTF8NoPreamble = new UTF8Encoding();

MemoryStream stm = new MemoryStream();
StreamWriter wr = new StreamWriter(stm, UTF8NoPreamble);
XmlSerializer xs = new XmlSerializer(typeof(DataObject));
xs.Serialize(wr, data);

string str = UTF8NoPreamble.GetString(stm.GetBuffer(),0,
(int)stm.Length);

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"BrentonMCA" <Br********@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.com...
Chris,

Thank you. It appears to do the job. Just one other query - I am a bit
confused about the first character returned.

When I move the mouse cursor over the variable that contains the required
text, the first character is displayed as unprintable and is followed by
"<?xml..."

When I display the value of the variable in the Command window, the first
character is displayed as a dot, much as I would expect to see chr(250). But asc(textString.substring(0,1)) prduces 63 (= asc("?")).

When I read a hex dump of the string, the first character is interpreted as 3F (="?").

I guess I can just ignore the first character, but I am curious about these inconsistencies.

Can you explain them?

Brenton

"Chris Taylor" wrote:
Hi,

You can serialize the objects to a memory stream and then send the bytes in the memory stream buffer over the TCP connection. Assuming you are using
XmlSerializer the following snippet should get you going.

MemoryStream stm = new MemoryStream();
StreamWriter wr = new StreamWriter(stm, System.Text.Encoding.UTF8);
XmlSerializer xs = new XmlSerializer(typeof(DataObject));
xs.Serialize(wr, data);

now the the serialized data can be accessed via the stm.GetBuffer(), the
length of the data contained in the buffer can be accessed via the
stm.Length property.

string str = System.Text.Encoding.UTF8.GetString(stm.GetBuffer( ),0,
(int)stm.Length);

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"BrentonMCA" <Br********@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
I want to be able to serialise a .Net object and send the resulting XML by TCP. How can I extract the serialised XML as a string?


Nov 12 '05 #4
Great.

Thank you

"Chris Taylor" wrote:
Hi,

What you are seeing is the unicode preamble, this is a byte order marker
prepended to unicode files so that an editor can identify the file as
unicode including the unicode transformation and the byte order. To see this
open a text file in notepad and save it in a unicode format and open the
file with a binary editor and you will notice this preamble.

The preamble for UTF-8 is 0xEF, 0xBB, 0XBF, for Unicode it is 0xFF, 0xFE. To
exclude this preamble from the encoding, create a new instance of the
UTFTEncoding and use that.

UTF8Encoding UTF8NoPreamble = new UTF8Encoding();

MemoryStream stm = new MemoryStream();
StreamWriter wr = new StreamWriter(stm, UTF8NoPreamble);
XmlSerializer xs = new XmlSerializer(typeof(DataObject));
xs.Serialize(wr, data);

string str = UTF8NoPreamble.GetString(stm.GetBuffer(),0,
(int)stm.Length);

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"BrentonMCA" <Br********@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.com...
Chris,

Thank you. It appears to do the job. Just one other query - I am a bit
confused about the first character returned.

When I move the mouse cursor over the variable that contains the required
text, the first character is displayed as unprintable and is followed by
"<?xml..."

When I display the value of the variable in the Command window, the first
character is displayed as a dot, much as I would expect to see chr(250).

But
asc(textString.substring(0,1)) prduces 63 (= asc("?")).

When I read a hex dump of the string, the first character is interpreted

as
3F (="?").

I guess I can just ignore the first character, but I am curious about

these
inconsistencies.

Can you explain them?

Brenton

"Chris Taylor" wrote:
Hi,

You can serialize the objects to a memory stream and then send the bytes in the memory stream buffer over the TCP connection. Assuming you are using
XmlSerializer the following snippet should get you going.

MemoryStream stm = new MemoryStream();
StreamWriter wr = new StreamWriter(stm, System.Text.Encoding.UTF8);
XmlSerializer xs = new XmlSerializer(typeof(DataObject));
xs.Serialize(wr, data);

now the the serialized data can be accessed via the stm.GetBuffer(), the
length of the data contained in the buffer can be accessed via the
stm.Length property.

string str = System.Text.Encoding.UTF8.GetString(stm.GetBuffer( ),0,
(int)stm.Length);

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"BrentonMCA" <Br********@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
> I want to be able to serialise a .Net object and send the resulting XML by > TCP. How can I extract the serialised XML as a string?


Nov 12 '05 #5

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

Similar topics

2
by: Gabriel Gudenus | last post by:
Hi Group! I am just working on a client-server network application which exchanges different Message Classes. (ObjectInputStream and ObjectOutputStream) Now i also would like to send a file...
2
by: Chuck Marques | last post by:
I'm trying to implement sending an object through messaging, but I receive an error stating I can't deserialize the object. Any clues as to why. I have the following: <Serializable()> Public...
0
by: Pierre | last post by:
Hi, I'm trying to select specific nodes from a XmlDocument filled with a serialized object and to insert these nodes into another XmlDocument. The object is well serialized (see below). From a...
7
by: Neal Andrews | last post by:
Hi All, Does anyone know how to stop Events from being serialized in a class that uses the <Serializable()> attribute? I have tried using the <NonSerialized()> attribute but for some bizarre...
1
by: Brian Mitchell | last post by:
I have an interesting problem when I try to send bulk data over a UDP connection. When my message length is over 1,400 bytes I get the error "A message sent on a datagram socket was larger than the...
9
by: Nick Gilbert | last post by:
Hi, I'm trying to create a system whereby my desktop application submits it's order to an online server using a webservice (pretty standard!). So, I added a new project to my solution to...
1
by: hazz | last post by:
if I think about efficiently sending data back and forth through a webservice for a 'typical' application (in this case smart client using Composite UI application block) what questions do I ask...
9
by: Miro | last post by:
VB 2003 at the end of the code, this works great. bytCommand = Encoding.ASCII.GetBytes("testing hello send text") udpClient.Send(bytCommand, bytCommand.Length) and this recieves it Dim...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
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: 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:
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
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,...
0
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...
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.