XmlWriter编写空的stringbuilder

时间:2018-02-26 18:34:20

标签: c# linq stringbuilder xelement xmlwriter

我有以下代码"工作"对于我的测试,但我不喜欢格式化:

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在兔子洞里写下来并继续追踪它。

1 个答案:

答案 0 :(得分:1)

您的代码不会打印任何内容,因为当您调用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语句中强制使用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语句,它似乎是毫无意义和冗余的(它将被再次调用)。