用Linq到Xml间隔输出 [英] Spacing out output with Linq to Xml
本文介绍了用Linq到Xml间隔输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用Linq to Xml强制某些节点之间的额外间距?我希望输出以下内容:
How is it possible to force extra spacing between some nodes using Linq to Xml? I am looking to output the following:
<root>
<group>
<leaf />
</group>
<group>
<leaf />
</group>
</root>
通过添加Empty XText,它只会破坏格式.
By adding Empty XText, it only destroys the formatting.
var root =
new XElement("root",
new XText(""),
new XElement("group",
new XElement("leaf")),
new XText(""),
new XElement("group",
new XElement("leaf")),
new XText(""));
Console.WriteLine(root.ToString());
导致
<root><group><child /></group><group><child /></group></root>
推荐答案
这是一个解决方案,但它并不漂亮...
This is a solution but it´s not beautiful...
将XText更改为XComment并执行类似的操作...
Change XText to XComment and do something like this...
var root =
new XElement("root",
new XComment(""),
new XElement("group",
new XElement("leaf")),
new XComment(""),
new XElement("group",
new XElement("leaf")),
new XComment(""));
Console.WriteLine(XElementToText(root));
private string XElementToText(XElement element)
{
var sb = new StringBuilder();
using (var writer = XmlWriter.Create(sb,
new XmlWriterSettings {Indent = true}))
{
element.WriteTo(writer);
}
return sb.ToString().Replace("<!---->", string.Empty);
}
Fogott逃脱不到...
Fogott to escape lesser than ...
这篇关于用Linq到Xml间隔输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文