473,246 Members | 1,703 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,246 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 6171
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.