XmlDocument.Save 省略元素

发布于 2024-08-12 16:13:32 字数 1194 浏览 6 评论 0原文

我有以下在运行时使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尬尬 2024-08-19 16:13:32

两种发送数据的方式都没有问题。问题在于您在服务器端读取 XML 的方式。例如,使用第一种方法和以下侦听器,我能够获取整个 XML:

class Program
{
    static void Main(string[] args)
    {
        var listener = new TcpListener(IPAddress.Loopback, 9999);
        listener.Start();
        while (true)
        {
            var client = listener.AcceptTcpClient();
            using (var stream = client.GetStream())
            using (var reader = new StreamReader(stream))
            {
                Console.WriteLine(reader.ReadToEnd());
            }
        }
    }
}

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:

class Program
{
    static void Main(string[] args)
    {
        var listener = new TcpListener(IPAddress.Loopback, 9999);
        listener.Start();
        while (true)
        {
            var client = listener.AcceptTcpClient();
            using (var stream = client.GetStream())
            using (var reader = new StreamReader(stream))
            {
                Console.WriteLine(reader.ReadToEnd());
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文