XmlDocument.Save 省略元素
我有以下在运行时使用 XmlDocument
构建的 XML:
<?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 个元素( 和
)永远不存在。
但是,如果我使用以下代码,XML 将成功发送:
TcpClient client = ...
XmlDocument configDocument = ...
using (StreamWriter writer = new StreamWriter(client.GetStream()))
{
writer.AutoFlush = true;
writer.WriteLine(configDocument.OuterXml);
}
因此,我的问题是:有谁知道为什么 XmlDocument.Save()
在写入 流
?
I have the following XML that is built up at runtime using an 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>
I'm saving this XML to a TCP socket with the following code:
TcpClient client = ...
XmlDocument configDocument = ...
using (StreamWriter writer = new StreamWriter(client.GetStream()))
{
writer.AutoFlush = true;
configDocument.Save(writer);
writer.WriteLine();
}
But this causes the XML that is received by the other end of the socket to be truncated - the last 2 elements (</ClientList>
and </NewConfig>
) are never present.
However, if I use the following code, the XML is sent successfully:
TcpClient client = ...
XmlDocument configDocument = ...
using (StreamWriter writer = new StreamWriter(client.GetStream()))
{
writer.AutoFlush = true;
writer.WriteLine(configDocument.OuterXml);
}
My question therefore, is: Does anyone know why XmlDocument.Save()
seems to be ignoring the closing elements when writing to the Stream
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两种发送数据的方式都没有问题。问题在于您在服务器端读取 XML 的方式。例如,使用第一种方法和以下侦听器,我能够获取整个 XML:
There's nothing wrong with both ways of sending the data. What is wrong is the way you read the XML on the server side. For example using the first method and the following listener, I was able to get the entire XML: