XmlWriter 写入空 stringbuilder

XmlWriter writing empty stringbuilder

我有以下代码 "worked" 用于测试,但我不喜欢这种格式:

System.Diagnostics.Debug.WriteLine("----------------------------- Start 1 ----------------------------");
using (var sw = new Utf8StringWriter())
//StringBuilder sb = new StringBuilder();
//using (XmlWriter xw = XmlWriter.Create(sb, new XmlWriterSettings() { Encoding = Encoding.UTF8, Indent = true, IndentChars = "\t" }))
{
    XDocument d = new XDocument(new XDeclaration(version: "1.0", encoding: "UTF-8", standalone: "no"));
    XElement DMTStandardIF = new XElement("DMTStandardIF", new XAttribute("version", "1.00"));
    d.Add(DMTStandardIF);
    XElement last = DMTStandardIF;
    last.Add(last = new XElement("Thing1", "thing 1 stuff"));
    last.Add(null);
    last.Add(last = new XElement("Thing2", "thing 2 stuff"));
    last.Add(null);
    d.Save(sw);
    //d.WriteTo(xw);
    System.Diagnostics.Debug.WriteLine(sw);
    //System.Diagnostics.Debug.WriteLine(sb.ToString());
}

// 所以我将其更改为以下内容,不输出任何内容; xw有内容但sb为空

System.Diagnostics.Debug.WriteLine("----------------------------- Start 2 ----------------------------");
StringBuilder sb = new StringBuilder();
using (XmlWriter xw = XmlWriter.Create(sb, new XmlWriterSettings() { Encoding = Encoding.UTF8, Indent = true, IndentChars = "\t" }))
{
    XDocument d = new XDocument(new XDeclaration(version: "1.0", encoding: "UTF-8", standalone: "no"));
    XElement DMTStandardIF = new XElement("DMTStandardIF", new XAttribute("version", "1.00"));
    d.Add(DMTStandardIF);
    XElement last = DMTStandardIF;
    last.Add(last = new XElement("Thing1", "thing 1 stuff"));
    last.Add(null);
    last.Add(last = new XElement("Thing2", "thing 2 stuff"));
    last.Add(null);
    d.WriteTo(xw);
    //d.Save(xw); // I tried this too and .. empty as well
    System.Diagnostics.Debug.WriteLine(sb.ToString());
}
System.Diagnostics.Debug.WriteLine("------------------------------ Done ------------------------------");

如果您运行满足以下条件,您将获得:

----------------------------- Start 1 ----------------------------
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<DMTStandardIF version="1.00">
  <Thing1>thing 1 stuff<Thing2>thing 2 stuff</Thing2></Thing1>
</DMTStandardIF>
----------------------------- Start 2 ----------------------------

------------------------------ Done ------------------------------

为什么第二个版本什么都不输出?我该如何解决?

这一切都开始了,因为我想测试如果我尝试 myXElement.Add(null) 会发生什么,我已经回答了这个问题,但我看到 XmlWriter 在兔子洞里写空,然后去追它。

您的代码没有打印任何内容,因为当您调用 Debug.WriteLine 时,StringBuilder 尚未从 XmlWriter 接收到缓冲区。当您的代码从 using 语句

退出时,会发生这种情况(可能是通过调用 xw.Flush())

只需将 StringBuilder 的打印移到 using 语句之外

using (XmlWriter xw = XmlWriter.Create(sb, new XmlWriterSettings() { Encoding = Encoding.UTF8, Indent = true, IndentChars = "\t" }))
{
    ....
    d.WriteTo(xw);
}
System.Diagnostics.Debug.WriteLine(sb.ToString());
System.Diagnostics.Debug.WriteLine("------------------------------ Done --------------------");

或者,您可以在打印 StringBuilder 之前在 using 语句中的 XmlWriter 上强制执行 Flush

using (XmlWriter xw = XmlWriter.Create(sb, new XmlWriterSettings() { Encoding = Encoding.UTF8, Indent = true, IndentChars = "\t" }))
{
    ....
    d.WriteTo(xw);
    xw.Flush();
    System.Diagnostics.Debug.WriteLine(sb.ToString());
}
System.Diagnostics.Debug.WriteLine("------------------------------ Done --------------------");

但在这种情况下,考虑到代码仅在下一行退出 using 语句,这似乎毫无意义且多余(将再次调用)。