Xml over socket sample code

Hi,
does anyone have a code sample to receive an xml document over a tcp socket?
Until now I just send plain text messages with a \r\n endmarker, but how do I know the complete xml document is received?

If the document is well formed you can just look for the end of the root element…

If you have control over the server side code, you can transmit in the first packet the number of bytes in the file.