在 C# 中,如何将 XmlNode 转换为带有缩进的字符串? (不循环)

发布于 2024-11-16 16:19:04 字数 1024 浏览 3 评论 0原文

这一定是一个非常简单的问题,但我就是找不到答案。

我有一个 XmlNode,我想要做的就是将该节点输出为字符串,并保持完整的缩进(制表符或空格)以提供更好的可读性。

到目前为止,我尝试了 XmlWriter、XmlTextWriter、XmlDocument、XmlReader。

  • 我尝试了 XmlDocument 中的 PreserveWhitespace 但无法让 XmlDocument 输出我的节点。
  • 我尝试了 XmlTextWriter 中的 Formatting = Formatting.Indented 属性,但我不知道如何将内容输出到字符串。

将 XmlNode 输出为不带缩进的字符串很容易。我只做 XmlNode.OuterXml。我如何获得那里的缩进?

我想在不循环 XmlNode 并使用强力添加空格的情况下执行此操作,因为我认为应该有一种更简单的方法。

谢谢。

编辑:对于未来的读者,答案如下:

  var xmlNode = is some object of type XmlNode

  using (var sw = new StringWriter())
  {
      using (var xw = new XmlTextWriter(sw))
      {
        xw.Formatting = Formatting.Indented;
        xw.Indentation = 2; //default is 1. I used 2 to make the indents larger.

        xmlNode.WriteTo(xw);
      }
      return sw.ToString(); //The node, as a string, with indents!
  }

我需要这样做的原因是通过语法突出显示输出节点的 xml。我使用 AvalonEdit 突出显示 xml,将突出显示的文本输出到 html,然后将 html 转换为可以在 RichTextBox 中显示的 FlowDocument。

This has got to be such a simple question but I just can't get the answer.

I have an XmlNode and all I want to do is output this node, as a string, with indentations (tabs or spaces) intact to provide better readability.

So far I tried XmlWriter, XmlTextWriter, XmlDocument, XmlReader.

  • I tried the PreserveWhitespace in XmlDocument but I couldn't get the XmlDocument to output my node.
  • I tried the Formatting = Formatting.Indented property in XmlTextWriter but I couldn't figure out how to output the contents to string.

To output the XmlNode as string WITHOUT indentation is easy. I just do XmlNode.OuterXml. How do I get the indentations in there?

I want to do this without looping through the XmlNode and using brute force to add whitespace, because I think there should be a simpler way.

Thanks.

Edit: For future readers, here is the answer:

  var xmlNode = is some object of type XmlNode

  using (var sw = new StringWriter())
  {
      using (var xw = new XmlTextWriter(sw))
      {
        xw.Formatting = Formatting.Indented;
        xw.Indentation = 2; //default is 1. I used 2 to make the indents larger.

        xmlNode.WriteTo(xw);
      }
      return sw.ToString(); //The node, as a string, with indents!
  }

The reason I needed to do this was output the node's xml with syntax highlighting. I used AvalonEdit to highlight the xml, outputted the highlighted text to html, then converted the html to a FlowDocument which could be displayed in a RichTextBox.

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

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

发布评论

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

评论(2

漫雪独思 2024-11-23 16:19:04

您使用 XMLTextWriter 的方法是正确的,您只需使用 StringWriter 作为基本流。这里有一些很好的答案关于如何实现这一点。如果您的编码需要为 UTF-8,请特别注意第二个答案。

编辑:

如果您需要在多个位置执行此操作,则编写一个扩展方法来重载 XmlNode 上的 ToString() 是很简单的:

public static class MyExtensions
{
    public static string ToString(this System.Xml.XmlNode node, int indentation)
    {
        using (var sw = new System.IO.StringWriter())
        {
            using (var xw = new System.Xml.XmlTextWriter(sw))
            {
                xw.Formatting = System.Xml.Formatting.Indented;
                xw.Indentation = indentation;
                node.WriteContentTo(xw);
            }
            return sw.ToString();
        }
    }
}

You were on the right path with the XMLTextWriter, you simply need to use a StringWriter as the base stream. Here are a few good answers on how this is accomplished. Pay particular attention to the second answer, if your encoding needs to be UTF-8.

Edit:

If you need to do this in multiple places, it is trivial to write an extension method to overload a ToString() on XmlNode:

public static class MyExtensions
{
    public static string ToString(this System.Xml.XmlNode node, int indentation)
    {
        using (var sw = new System.IO.StringWriter())
        {
            using (var xw = new System.Xml.XmlTextWriter(sw))
            {
                xw.Formatting = System.Xml.Formatting.Indented;
                xw.Indentation = indentation;
                node.WriteContentTo(xw);
            }
            return sw.ToString();
        }
    }
}
柠栀 2024-11-23 16:19:04

如果你不关心内存或性能,最简单的是:

    XElement.Parse(xmlNode.OuterXml).ToString()

If you don't care about memory or performance, the simplest thing is:

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