如何以编程方式格式化 HTML 字符串

发布于 2024-07-11 16:18:27 字数 409 浏览 5 评论 0原文

我在字符串中有未格式化的 html。

我正在尝试很好地格式化它并将格式化的 html 输出回字符串中。 我一直在尝试使用 System.Web.UI.HtmlTextWriter 无济于事:

System.IO.StringWriter wString = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter wHtml = new System.Web.UI.HtmlTextWriter(wString);

wHtml.Write(sMyUnformattedHtml);

string sMyFormattedHtml = wString.ToString();

我得到的只是未格式化的 html,是否有可能实现我在这里尝试做的事情?

I've got unformatted html in a string.

I am trying to format it nicely and output the formatted html back into a string.
I've been trying to use The System.Web.UI.HtmlTextWriter to no avail:

System.IO.StringWriter wString = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter wHtml = new System.Web.UI.HtmlTextWriter(wString);

wHtml.Write(sMyUnformattedHtml);

string sMyFormattedHtml = wString.ToString();

All I get is the unformatted html, is it possible to achieve what I'm trying to do here?

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

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

发布评论

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

评论(4

神也荒唐 2024-07-18 16:18:27

这是一个完全执行此操作的函数:

    // Attractively format the XML with consistant indentation.

    public static String PrettyPrint(String XML)
    {
        String Result = "";

        using (MemoryStream MS = new MemoryStream())
        {
            using (XmlTextWriter W = new XmlTextWriter(MS, Encoding.Unicode))
            {
                XmlDocument D = new XmlDocument();

                try
                {
                    // Load the XmlDocument with the XML.
                    D.LoadXml(XML);

                    W.Formatting = Formatting.Indented;

                    // Write the XML into a formatting XmlTextWriter
                    D.WriteContentTo(W);
                    W.Flush();
                    MS.Flush();

                    // Have to rewind the MemoryStream in order to read
                    // its contents.
                    MS.Position = 0;

                    // Read MemoryStream contents into a StreamReader.
                    StreamReader SR = new StreamReader(MS);

                    // Extract the text from the StreamReader.
                    String FormattedXML = SR.ReadToEnd();

                    Result = FormattedXML;
                }
                catch (XmlException ex)
                {
                    Result= ex.ToString();
                }

                W.Close();
            }
            MS.Close();
        }
        Debug.WriteLine(Result);
        return Result;
    }

Here's a function that does exactly that:

    // Attractively format the XML with consistant indentation.

    public static String PrettyPrint(String XML)
    {
        String Result = "";

        using (MemoryStream MS = new MemoryStream())
        {
            using (XmlTextWriter W = new XmlTextWriter(MS, Encoding.Unicode))
            {
                XmlDocument D = new XmlDocument();

                try
                {
                    // Load the XmlDocument with the XML.
                    D.LoadXml(XML);

                    W.Formatting = Formatting.Indented;

                    // Write the XML into a formatting XmlTextWriter
                    D.WriteContentTo(W);
                    W.Flush();
                    MS.Flush();

                    // Have to rewind the MemoryStream in order to read
                    // its contents.
                    MS.Position = 0;

                    // Read MemoryStream contents into a StreamReader.
                    StreamReader SR = new StreamReader(MS);

                    // Extract the text from the StreamReader.
                    String FormattedXML = SR.ReadToEnd();

                    Result = FormattedXML;
                }
                catch (XmlException ex)
                {
                    Result= ex.ToString();
                }

                W.Close();
            }
            MS.Close();
        }
        Debug.WriteLine(Result);
        return Result;
    }
痴骨ら 2024-07-18 16:18:27

您可以将其传递到外部 tidy 或使用 XmlTextWriter 如果您愿意使用 XHTML 而不是 HTML。

You can pass it to tidy externally or use XmlTextWriter if you are willing to use XHTML instead of HTML.

无语# 2024-07-18 16:18:27

框架中没有任何内容可以满足您的要求。

如果 HTML 片段是有效的 XML,您可以将其加载到 XmlDocument 中并编写一些代码来遍历它并按照您想要的格式输出它。

There's nothing in the framework that will do what you want.

If the HTML fragment is valid XML you could load it into an XmlDocument and write some code to traverse it and output it formatted how you want.

陌路黄昏 2024-07-18 16:18:27

使用 EFTidyNet,Tidy 的托管 .NET 包装器。 它比使用批处理文件调用 Tidy 简单得多,而且速度也快得多。

Tidy 可以清理您的 HTML 并使其看起来更漂亮,并将其转换为有效的 HTML 或 XHTML。

Use EFTidyNet, the managed .NET wrapper for Tidy. It is much simpler than using a batch file to call Tidy and also a lot faster.

Tidy can clean up your HTML and make it look nice, as well as turning it into valid HTML or XHTML.

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