使用xmltextwwriter时截断流中的输出

本文关键字:输出 xmltextwwriter 使用 | 更新日期: 2023-09-27 18:10:34

 using (Stream responseStream = new MemoryStream())
                {
                    try
                    {
                        XmlTextWriter xmlWriter;
                        xmlWriter = new XmlTextWriter(@"D:'DEmo.txt", new UTF8Encoding(true));
                        xmlWriter.Formatting = Formatting.Indented;
                        xmlWriter.IndentChar = ''t';
                        xmlWriter.Indentation = 1;
                        xmlWriter.WriteStartDocument();
                        xmlWriter.WriteStartElement("D", "multistatus", "DAV:");
                        xmlWriter.WriteAttributeString("xmlns:M", "urn:schemas-microsoft-com:");
                        foreach (IWebDAVStoreItem item in store.Root.Items)
                        {
                            if (item is Server.Stores.DiskStore.WebDAVDiskStoreDocument)
                            {
                                ((Server.Stores.DiskStore.WebDAVDiskStoreDocument)item).AddProperties(xmlWriter);
                            }
                        }
                        xmlWriter.WriteEndElement();
                        xmlWriter.WriteEndDocument();
                        responseStream.Flush();
                        responseStream.Position = 0;
                        byte[] bytes = new byte[responseStream.Length];
                        responseStream.Read(bytes, 0, (int)responseStream.Length);
                        output = Encoding.ASCII.GetString(bytes); 
                        //output = (new StreamReader(responseStream)).ReadToEnd();
                        logger.Log(LogLevel.Warning, "'n" + output);
                        responseStream.Close();
                    }
                    catch(Exception ex) {
                        logger.Log(LogLevel.Warning, "'n" + ex.Message);
                    }
                }

我试图从上面的代码生成XML,但从MemoryStream读取不显示完整的XML。XML内容的最后一部分总是被截断,无论XML有多大,我都不知道发生了什么。截断后的输出如下所示:

     <?xml version="1.0" encoding="utf-8"?>
        <D:multistatus xmlns:M="urn:schemas-microsoft-com:" xmlns:D="DAV:">         
    <D:response>
            <D:href>/ANSI-ISO C++ Professional Programmers Handbook.pdf</D:href>
            <D:propstat>
                <D:status>HTTP/1.1 200 OK</D:status>
                <D:prop>        
<M:Win32CreationTime>2014-06-28T14:30:09</M:Win32CreationTime>
                    <M:Win32LastAccess>2014-06-28T14:30:09</M:Win32LastAccess>
                        <M:Win32LastModifiedTime>2014-06-28T14:30:09</M:Win32La

使用xmltextwwriter时截断流中的输出

部分数据可能仍然保存在XmlTextWriter的底层流缓冲区中。在你写完之后,你应该调用XmlTextWriterClose方法。如果您希望稍后继续写入,但仍然希望确保所有数据都已写入,则可以使用Flush