XMLSerializer, socket

K

kimtherkelsen

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
K

kimtherkelsen

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


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]
- (e-mail address removed)




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 -
 
M

Moty Michaely

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top