473,385 Members | 1,409 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,385 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 6177
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.