469,160 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

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 5929
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gabriel Gudenus | last post: by
2 posts views Thread by Chuck Marques | last post: by
7 posts views Thread by Neal Andrews | last post: by
1 post views Thread by Brian Mitchell | last post: by
9 posts views Thread by Nick Gilbert | last post: by
9 posts views Thread by Miro | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.