如何从 C# 中的 XDocument 创建缩进的 XML 字符串?

发布于 2024-08-23 21:12:33 字数 165 浏览 5 评论 0原文

我有一个 XDocument 对象,并且 ToString() 方法返回没有任何缩进的 XML。如何从中创建包含缩进 XML 的字符串?

编辑:我问如何创建内存中的字符串而不是写入文件。

编辑:看起来我不小心在这里问了一个技巧问题... ToString() 确实返回缩进的 XML。

I have an XDocument object and the ToString() method returns XML without any indentation. How do I create a string from this containing indented XML?

edit: I'm asking how to create an in memory string rather than writing out to a file.

edit: Looks like I accidentally asked a trick question here... ToString() does return indented XML.

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

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

发布评论

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

评论(4

瀞厅☆埖开 2024-08-30 21:12:33
XDocument doc = XDocument.Parse(xmlString);
string indented = doc.ToString();
XDocument doc = XDocument.Parse(xmlString);
string indented = doc.ToString();
冷情 2024-08-30 21:12:33

来自此处

XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>");

// Save the document to a file and auto-indent the output.
XmlTextWriter writer = new XmlTextWriter("data.xml",null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);

From here

XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>");

// Save the document to a file and auto-indent the output.
XmlTextWriter writer = new XmlTextWriter("data.xml",null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
梦纸 2024-08-30 21:12:33

只是同一汤的另一种口味...;-)

StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xw.Formatting = Formatting.Indented;
doc.WriteTo(xw);
Console.WriteLine(sw.ToString());

编辑:感谢 John Saunders。这是一个应该更好地符合 在 MSDN 上创建 XML 编写器 的版本。

using System;
using System.Text;
using System.Xml;
using System.Xml.Linq;

class Program
{
    static void Main(string[] args)
    {
        XDocument doc = new XDocument(
        new XComment("This is a comment"),
        new XElement("Root",
            new XElement("Child1", "data1"),
            new XElement("Child2", "data2")
            )
            );

        var builder = new StringBuilder();
        var settings = new XmlWriterSettings()
        {
            Indent = true
        };
        using (var writer = XmlWriter.Create(builder, settings))
        {
            doc.WriteTo(writer);
        }
        Console.WriteLine(builder.ToString());
    }
}

Just one more flavor of the same soup... ;-)

StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xw.Formatting = Formatting.Indented;
doc.WriteTo(xw);
Console.WriteLine(sw.ToString());

Edit: thanks to John Saunders. Here is a version that should better conform to Creating XML Writers on MSDN.

using System;
using System.Text;
using System.Xml;
using System.Xml.Linq;

class Program
{
    static void Main(string[] args)
    {
        XDocument doc = new XDocument(
        new XComment("This is a comment"),
        new XElement("Root",
            new XElement("Child1", "data1"),
            new XElement("Child2", "data2")
            )
            );

        var builder = new StringBuilder();
        var settings = new XmlWriterSettings()
        {
            Indent = true
        };
        using (var writer = XmlWriter.Create(builder, settings))
        {
            doc.WriteTo(writer);
        }
        Console.WriteLine(builder.ToString());
    }
}
以往的大感动 2024-08-30 21:12:33

要使用 XDocument(而不是 XmlDocument)创建字符串,您可以使用:

        XDocument doc = new XDocument(
            new XComment("This is a comment"),
            new XElement("Root",
                new XElement("Child1", "data1"),
                new XElement("Child2", "data2")
            )
        );

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        StringBuilder sb = new StringBuilder();
        using (XmlWriter writer = XmlTextWriter.Create(sb, settings)) {
            doc.WriteTo(writer);
            writer.Flush();
        }
        string outputXml = sb.ToString();

Edit: 更新为使用 XmlWriter.CreateStringBuilder和良好的形式(使用)。

To create a string using an XDocument (rather than an XmlDocument), you can use:

        XDocument doc = new XDocument(
            new XComment("This is a comment"),
            new XElement("Root",
                new XElement("Child1", "data1"),
                new XElement("Child2", "data2")
            )
        );

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        StringBuilder sb = new StringBuilder();
        using (XmlWriter writer = XmlTextWriter.Create(sb, settings)) {
            doc.WriteTo(writer);
            writer.Flush();
        }
        string outputXml = sb.ToString();

Edit: Updated to use XmlWriter.Create and a StringBuilder and good form (using).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文