我正在从 TcpClient 流中读取 XDocument。在实现时,我想知道 XDocument.Load(Stream) 是如何知道文档何时完成的?
我脑海中闪过几个想法,比如接收到流结束或没有更多可用字节。但现在看起来更多的是 Load() 在收到 EndElement 时完成。
有人知道 Load(Stream) 内部是如何工作的吗?
谢谢 汤姆
请您参考如下方法:
很容易证明它读取了结束元素 - 您只需要创建一个 XML 文件,其中包含结束元素之后的数据,使其无效:
<root>
<child />
</root>
More stuff
如果你加载它,你会得到这样的异常:
Unhandled Exception: System.Xml.XmlException: Data at the root level is invalid.
Line 5, position 1.
我强烈希望它读到流的末尾。
