我在运行时使用以下 XML 构建XmlDocument
:
<?xml version="1.0" standalone="yes"?>
<NewConfig xmlns="http://tempuri.org/NewConfig.xsd">
<SystemReference xmlns="">
<ID>1</ID>
<Name>CountryName</Name>
</SystemReference>
<ClientList xmlns="">
<Type>Private</Type>
<!-- elements omitted... -->
<VAT>1234567890</VAT>
</ClientList>
</NewConfig>
我使用以下代码将此 XML 保存到 TCP 套接字:
TcpClient client = ...
XmlDocument configDocument = ...
using (StreamWriter writer = new StreamWriter(client.GetStream()))
{
writer.AutoFlush = true;
configDocument.Save(writer);
writer.WriteLine();
}
但这会导致套接字另一端接收到的 XML 被截断 - 最后 2 个元素 (</ClientList>
和</NewConfig>
) 永远不会出现。
但是,如果我使用以下代码,则 XML 发送成功:
TcpClient client = ...
XmlDocument configDocument = ...
using (StreamWriter writer = new StreamWriter(client.GetStream()))
{
writer.AutoFlush = true;
writer.WriteLine(configDocument.OuterXml);
}
因此,我的问题是:有谁知道为什么XmlDocument.Save()
在写信时似乎忽略了结束元素Stream
?