473,385 Members | 1,829 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.

XMLSerializer, socket

Hi,
I want to send XML data from a server to some clients over a network
connection using the TCP/IP protocol. If I send the XMLs as byte
arrays I need to insert header information in the data to distinguish
the XMLs from each other in the stream of data. Is there any way to
avoid this (for instance by sending SOAP telegrams))?

I have tried using the XMLSerializer.Serialize(stream) to serialize
the XML telegrams and send them over the socket connection but when I
want to use the XMLSerializer.Deserialize(stream) to read the sent
telegrams it either newer finishes or returns a xml parse error.

//Send the XML
ShoppingList myList = new ShoppingList();
myList.AddItem(new ShoppingItem("eggs", 1.49));
myList.AddItem(new ShoppingItem("ground beef", 3.69));
myList.AddItem(new ShoppingItem("bread", 0.89));

XmlSerializer s = new XmlSerializer(typeof(ShoppingList));
stream = new NetworkStream(m_socWorker);
s.Serialize(stream, myList);
//Receive the XML
// Deserialization
ShoppingList newList;
XmlSerializer s = new XmlSerializer(typeof(ShoppingList));
newList = (ShoppingList)s.Deserialize(stream);

What am I doing wrong? Will the Deserialize() method be able to
separate the XML telegrams in the stream automatically?

Best regards,
Kim

May 17 '07 #1
3 3280
Unfortunately, no. What you should do is before each serialized type
you want to send, send the length of the XML document you are going to send.
Then, on the other side, take note of the length, and only read that much
into a byte array, or something of the sort, and then pass that to the
XmlSerializer. Finally, repeat for all other instances.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ki***********@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
Hi,
I want to send XML data from a server to some clients over a network
connection using the TCP/IP protocol. If I send the XMLs as byte
arrays I need to insert header information in the data to distinguish
the XMLs from each other in the stream of data. Is there any way to
avoid this (for instance by sending SOAP telegrams))?

I have tried using the XMLSerializer.Serialize(stream) to serialize
the XML telegrams and send them over the socket connection but when I
want to use the XMLSerializer.Deserialize(stream) to read the sent
telegrams it either newer finishes or returns a xml parse error.

//Send the XML
ShoppingList myList = new ShoppingList();
myList.AddItem(new ShoppingItem("eggs", 1.49));
myList.AddItem(new ShoppingItem("ground beef", 3.69));
myList.AddItem(new ShoppingItem("bread", 0.89));

XmlSerializer s = new XmlSerializer(typeof(ShoppingList));
stream = new NetworkStream(m_socWorker);
s.Serialize(stream, myList);
//Receive the XML
// Deserialization
ShoppingList newList;
XmlSerializer s = new XmlSerializer(typeof(ShoppingList));
newList = (ShoppingList)s.Deserialize(stream);

What am I doing wrong? Will the Deserialize() method be able to
separate the XML telegrams in the stream automatically?

Best regards,
Kim

May 17 '07 #2
Thank you for your answer.

I am a bit disappointed that the XMLSerializer.Deserialize(Stream)
method is actually more or less useless when trying to read from a
Networkstream. It would be nice to have a some sort of class that you
could just wrap around the whole thing and then you could send the
objects from one computer and receive the objects on another computer
as an object array without having to worry about separating the data.

Best regards,
Kim Therkelsen
On 17 Maj, 22:58, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Unfortunately, no. What you should do is before each serialized type
you want to send, send the length of the XML document you are going to send.
Then, on the other side, take note of the length, and only read that much
into a byte array, or something of the sort, and then pass that to the
XmlSerializer. Finally, repeat for all other instances.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

<kimtherkel...@gmail.comwrote in message

news:11**********************@e65g2000hsc.googlegr oups.com...
Hi,
I want to send XML data from a server to some clients over a network
connection using the TCP/IP protocol. If I send the XMLs as byte
arrays I need to insert header information in the data to distinguish
the XMLs from each other in the stream of data. Is there any way to
avoid this (for instance by sending SOAP telegrams))?
I have tried using the XMLSerializer.Serialize(stream) to serialize
the XML telegrams and send them over the socket connection but when I
want to use the XMLSerializer.Deserialize(stream) to read the sent
telegrams it either newer finishes or returns a xml parse error.
//Send the XML
ShoppingList myList = new ShoppingList();
myList.AddItem(new ShoppingItem("eggs", 1.49));
myList.AddItem(new ShoppingItem("ground beef", 3.69));
myList.AddItem(new ShoppingItem("bread", 0.89));
XmlSerializer s = new XmlSerializer(typeof(ShoppingList));
stream = new NetworkStream(m_socWorker);
s.Serialize(stream, myList);
//Receive the XML
// Deserialization
ShoppingList newList;
XmlSerializer s = new XmlSerializer(typeof(ShoppingList));
newList = (ShoppingList)s.Deserialize(stream);
What am I doing wrong? Will the Deserialize() method be able to
separate the XML telegrams in the stream automatically?
Best regards,
Kim- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -

May 18 '07 #3
On May 18, 9:31 am, "kimtherkel...@gmail.com"
<kimtherkel...@gmail.comwrote:
Thank you for your answer.

I am a bit disappointed that the XMLSerializer.Deserialize(Stream)
method is actually more or less useless when trying to read from a
Networkstream. It would be nice to have a some sort of class that you
could just wrap around the whole thing and then you could send the
objects from one computer and receive the objects on another computer
as an object array without having to worry about separating the data.

Best regards,
Kim Therkelsen

On 17 Maj, 22:58, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guard.caspershouse.comwrote:
Unfortunately, no. What you should do is before each serialized type
you want to send, send the length of the XML document you are going to send.
Then, on the other side, take note of the length, and only read that much
into a byte array, or something of the sort, and then pass that to the
XmlSerializer. Finally, repeat for all other instances.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
<kimtherkel...@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
Hi,
I want to send XML data from a server to some clients over a network
connection using the TCP/IP protocol. If I send the XMLs as byte
arrays I need to insert header information in the data to distinguish
the XMLs from each other in the stream of data. Is there any way to
avoid this (for instance by sending SOAP telegrams))?
I have tried using the XMLSerializer.Serialize(stream) to serialize
the XML telegrams and send them over the socket connection but when I
want to use the XMLSerializer.Deserialize(stream) to read the sent
telegrams it either newer finishes or returns a xml parse error.
//Send the XML
ShoppingList myList = new ShoppingList();
myList.AddItem(new ShoppingItem("eggs", 1.49));
myList.AddItem(new ShoppingItem("ground beef", 3.69));
myList.AddItem(new ShoppingItem("bread", 0.89));
XmlSerializer s = new XmlSerializer(typeof(ShoppingList));
stream = new NetworkStream(m_socWorker);
s.Serialize(stream, myList);
//Receive the XML
// Deserialization
ShoppingList newList;
XmlSerializer s = new XmlSerializer(typeof(ShoppingList));
newList = (ShoppingList)s.Deserialize(stream);
What am I doing wrong? Will the Deserialize() method be able to
separate the XML telegrams in the stream automatically?
Best regards,
Kim- Skjul tekst i anførselstegn -
- Vis tekst i anførselstegn -
Hi,

If you can manage both sides of the application, I would suggest using
Web Services Enhancements SOAP messaging over tcp.

If not, Nich just pointed the solution for you.

Cheers,

Moty

May 18 '07 #4

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

Similar topics

5
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not...
1
by: Bluetears76 | last post by:
Hi I have a hirachy of classes which are Message(base), then FileMessage and ChatMessage (extended) I want to serialize the objects and when i am deserizaling i dont know if i am getting...
3
by: Anthony Bouch | last post by:
Hi I've been reading using the XmlSerializer with custom collections. I've discovered that when serializing a custom collection (a class that implements ICollection, IList etc.) the...
3
by: Loui Mercieca | last post by:
Hi, I have created a class, named FormField , which basically contains two fields, name and value. I have set the tag before the class and the field is set as an XmlAttribute whil the name as...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
4
by: Steve Long | last post by:
Hello, I hope this is the right group to post this to. I'm trying to serialize a class I've written and I'd like to be able to serialze to both binary and xml formats. Binary serialization is...
0
by: William Stacey [MVP] | last post by:
Had a method that got some string info from mp3 tags in N files and serializes this class and deserializes at other side. Works ok except sometimes get chars that choke the XmlSerializer. After...
4
by: Ultrakorne | last post by:
hi, i have some problems with my client talk to my server... i am using xmlserializer to serialize object and send them to the other side of the connection. I need to send / recive by both client...
1
by: kimtherkelsen | last post by:
Hi, I want to send XML data from a server to some clients over a network connection using the TCP/IP protocol. If I send the XMLs as byte arrays I need to insert header information in the data to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.