Xml over tcp/ip

N

nyhetsgrupper

I have to applications communication over a socket. All messages are
sent by xml. What is the best method to read the response xml? Do I
need a delimiter to delimit the xml documents? Can I use something like
this:

NetworkStream netstream = client.GetStream();
XmlTextReader xmlreader = new XmlTextReader(netstream);

If I can, how can this work as the xml document can be split up into
many packages? Does the XmlTextReader take care of this automatically
without a delimiter?

Thank you!
 
M

Michael Nemtsev

Hello nyhetsgrupper,

See this sample http://www.codeproject.com/cs/internet/xml_communication_library.asp

n> I have to applications communication over a socket. All messages are
n> sent by xml. What is the best method to read the response xml? Do I
n> need a delimiter to delimit the xml documents? Can I use something
n> like this:
n>
n> NetworkStream netstream = client.GetStream();
n> XmlTextReader xmlreader = new XmlTextReader(netstream);
n> If I can, how can this work as the xml document can be split up into
n> many packages? Does the XmlTextReader take care of this automatically
n> without a delimiter?
n>
n> Thank you!
n>
---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
S

Samuel R. Neff

When sending XML over a persistent connection you need to be able to
identify the beginning and end of unique XML messages. This can be
expensive to do by examing the XML.

The sample Michael points to passes the size of the XML prior to the
XML so the receiving end knows how many bytes to read. Other samples
I've seen use null byte delimiters so after each xml message is
simplete it sends a null byte which makes idenitfying the delimiter
simple.

Macromedia Flash uses null-byte delimited XML messages as it's
standard for XMLSocket communications.

HTH,

Sam
 
0

0xB055

Samuel said:
The sample Michael points to passes the size of the XML prior to the
XML so the receiving end knows how many bytes to read. Other samples
I've seen use null byte delimiters so after each xml message is
simplete it sends a null byte which makes idenitfying the delimiter
simple.

Macromedia Flash uses null-byte delimited XML messages as it's
standard for XMLSocket communications.
null-byte delimiters only make sense when the XML data does not contain
binary data. i prefer solutions which do not add limitations to the data
that needs to be transmitted.

regards
alain
 
S

Samuel R. Neff

When is it acceptable to put binary data inside a text format ?!?

If "binary" data needs to be in an XML file then it's usually base64
encoded.

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
 
0

0xB055

Samuel said:
When is it acceptable to put binary data inside a text format ?!?

If "binary" data needs to be in an XML file then it's usually base64
encoded.

Sam
i agree. actually there are two types xs:hexBinary and xs:base64Binary
which allow to embed binary data into an XML document. still, i wouldn't
chose a solution that puts a limitation on my possibilities if there is
an alternative that doesn't.

alain
 
S

Samuel R. Neff

But one can certainly argue the putting binary data in an XML string
is not a real possibility in the first place. Try loading an XML file
with a null byte in the middle in any XML parser and I would be very
surprised if it worked (although I haven't tested it to be sure).

Anyways, using a header with a defined size is also a good way and
doesn't have the same limitation. Just be sure not to use any
**Reader/**Writer classes if you want to support binary in XML (does
..NET's XML provider even support that ?!?).

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
 
J

Jon Skeet [C# MVP]

0xB055 said:
null-byte delimiters only make sense when the XML data does not contain
binary data. i prefer solutions which do not add limitations to the data
that needs to be transmitted.

The XML data cannot legally contain a null character, so it doesn't add
any limitations as far as I can see. You may *encode* binary data
somehow (eg base 64 or hex) but that isn't going to represent it at the
XML level.

The problem with delimiters (IME) is that you need to look through the
data while you're receiving it to find the end. Prefixing the data with
its length is much nicer, assuming you know the size of the data before
you start transmitting. (If you don't, there's the possibility of
chunking, but that's making things a bit more complicated.)
 

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