How to format an HTML string programmatically

I have unformatted html in a string.

I am trying to format it nicely and output the formatted html back to string. I am trying to use 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 unformatted html, is it possible to achieve what I am trying to do here?

+1


source to share


4 answers


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

      

+2


source


You can pass it tidy externally, or use the XmlTextWriter if you want to use XHTML instead of HTML.



+2


source


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

Tidy can clean up your HTML and make it look pretty and also turn it into valid HTML or XHTML.

0


source


There is nothing within the framework that will do what you want.

If the HTML snippet is valid XML, you can load it into the XmlDocument and write code to move it around and output it in the format you want.

-1


source







All Articles