C# XmlWriter写入空stringbuilder
我有以下代码用于我的测试,但我不喜欢格式:C# XmlWriter写入空stringbuilder,c#,linq,stringbuilder,xelement,xmlwriter,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
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 ------------------------------
-------------------------------开始1----------------------------
第一件事第二件事
-----------------------------开始2----------------------------
------------------------------完成------------------------------
为什么第二个版本什么也不输出?我怎么修理它
这一切之所以开始,是因为我想测试如果我尝试myXElement.Add(null)会发生什么我已经回答了这个问题,但我看到XmlWriter在兔子洞里写空了,于是就去追它。你的代码没有打印任何东西,因为当你调用Debug.WriteLine时,StringBuilder还没有收到XmlWriter的缓冲区。当代码退出using语句时,就会发生这种情况(可能是通过调用xw.Flush())
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语句之外
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强制一个
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语句,它似乎是毫无意义和冗余的(将再次调用)。。。(如果允许的话,我会在4分钟内接受它。)不幸的是,它丢失了我的UTF-8并且无法修复我的格式;我希望在单独的行上有子标记。好的。想扩展该附录/替代解决方案以显示xw.Flush()?这个方法也可以,我刚刚也测试过。没错,更好的方法是让清除从退出
使用范围调用刷新,但它仍然很好地为下一代人演示。)