Html Agility Pack: make code look neat

前端 未结 3 1592
时光说笑
时光说笑 2020-12-06 17:33

Can I use Html Agility Pack to make the output look nicely indented, unnecessary white space stripped?

3条回答
  •  广开言路
    2020-12-06 18:31

    HAP is not going to give you the results you are after.

    Try using a .net wrapper for HtmlTidy such as the one found here

    using System;
    using System.IO;
    using System.Net;
    using Mark.Tidy;
    
    namespace CleanupHtml
    {
        /// 
        /// http://markbeaton.com/SoftwareInfo.aspx?ID=81a0ecd0-c41c-48da-8a39-f10c8aa3f931
        /// 
        internal class Program
        {
            private static void Main(string[] args)
            {
                string html =
                    new WebClient().DownloadString(
                        "http://stackoverflow.com/questions/2593147/html-agility-pack-make-code-look-neat/2610903#2610903");
    
                using (Document doc = new Document(html))
                {
                    doc.ShowWarnings = false;
                    doc.Quiet = true;
                    doc.OutputXhtml = true;
                    doc.OutputXml = true;
                    doc.IndentBlockElements = AutoBool.Yes;
                    doc.IndentAttributes = false;
                    doc.IndentCdata = true;
                    doc.AddVerticalSpace = false;
                    doc.WrapAt = 120;
    
                    doc.CleanAndRepair();
    
                    string output = doc.Save();
                    Console.WriteLine(output);
                    File.WriteAllText("output.htm", output);
                }
            }
        }
    }
    

    Results:

    
    
      
        
        
          Html Agility Pack: make code look neat - Stack Overflow
        
        
        
        
        
        
        
        
        
        
        
        
      
      
        
        
    vote up 1 vote down star

    Can I use Html Agility Pack to make the output look nicely indented, unnecessary white space stripped?

    flag

    50% accept rate
    what output? From where? some more details perhaps? – Sam Holder 2 days ago
    (reference) htmlagilitypack.codeplex.com/Wikipage – Gordon 2 days ago
    output = html code output – illdev 12 secs ago

    2 Answers

    vote up 0 vote down

    A variation of this question has been answered recently

    Basically the outcome of this was that while you can use HtmlAgilityPack to clean it up a bit by using the fix nested tags.

    The best solution is to use something called Tidy which is an application that was originally created by some developers at w3c and then made open source. Its the engine that powers the w3c validator as well.

    This article covers how to use it but you had to sign up (free) to view it:

    It seems like a legit article but its funny because nobody else seems to have covered this topic in the last six years...

    link|flag
    vote up 0 vote down

    Output as XHTML and run that through an XmlTextWriter

    link|flag

    Your Answer

    Get an OpenID
    or
    never shown

    Not the answer you're looking for? Browse other questions tagged or ask your own question.

提交回复
热议问题