使用 Linq to Xml 间隔输出

发布于 2024-08-15 14:01:05 字数 700 浏览 4 评论 0原文

如何使用 Linq to Xml 强制某些节点之间有额外的间距?我希望输出以下内容:

<root>

    <group>
        <leaf />
    </group>

    <group>
        <leaf />
    </group>

</root>

通过添加空 XText,它只会破坏格式。

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>

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>

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());

resulting in

<root><group><child /></group><group><child /></group></root>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏日浅笑〃 2024-08-22 14:01:05

这是一个解决方案,但它并不漂亮...

将 XText 更改为 XComment 并执行类似的操作...

 
    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 转义小于...

This is a solution but it´s not beautiful...

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);
    }

Edit: Fogott to escape lesser than ...

树深时见影 2024-08-22 14:01:05
using (var writer = XmlWriter.Create(
    Console.Out,
    new XmlWriterSettings { Indent = true })
{
    root.WriteTo(writer);
}
using (var writer = XmlWriter.Create(
    Console.Out,
    new XmlWriterSettings { Indent = true })
{
    root.WriteTo(writer);
}
墨小墨 2024-08-22 14:01:05

传递 XmlTextWriter (使用格式 设置为 Formatting.Indented) 为 root.WriteTo()

例如:

using(var writer = new XmlTextWriter(Console.Out));
{
    writer.Formatting = Formatting.Indented;
    root.WriteTo(writer);
}

Pass an XmlTextWriter (with Formatting set to Formatting.Indented) to root.WriteTo().

For example:

using(var writer = new XmlTextWriter(Console.Out));
{
    writer.Formatting = Formatting.Indented;
    root.WriteTo(writer);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文