How does XDocument.Load(Stream) detect end of XML message?

Im reading a XDocument from a TcpClient stream. While implementing, I was wondering how the XDocument.Load(Stream) knows when the document is completed?

Several ideas crossed my mind, like received stream end or no more Bytes available. But now it Looks more that the Load() completes when the EndElement is received.

Anybody knows how the Load(Stream) internals work?

Thanks Tom

Jon Skeet
people
quotationmark

It's easy to prove that it reads past the end element - you just need to create an XML file which contains data after the end element, making it invalid:

<root>
  <child />
</root>

More stuff

If you load that, you'll get an exception like this:

Unhandled Exception: System.Xml.XmlException: Data at the root level is invalid.
Line 5, position 1.

I would strongly expect it to read to the end of the stream.

people

See more on this question at Stackoverflow