html-agility-pack – Html Agility Pack:使代码看起来整洁

html-agility-pack – Html Agility Pack:使代码看起来整洁,第1张

概述我可以使用 Html Agility Pack使输出看起来很好地缩进,不必要的空白区域被剥离了吗? HAP不会给你你想要的结果. 尝试使用HtmlTidy的.net包装器,例如找到的一个here using System;using System.IO;using System.Net;using Mark.Tidy;namespace CleanupHtml{ /// <su 我可以使用 Html Agility Pack使输出看起来很好地缩进,不必要的空白区域被剥离了吗?解决方法 HAP不会给你你想要的结果.

尝试使用HTMLTIDy的.net包装器,例如找到的一个here

using System;using System.IO;using System.Net;using Mark.TIDy;namespace CleanupHTML{    /// <summary>    /// http://markbeaton.com/SoftwareInfo.aspx?ID=81a0ecd0-c41c-48da-8a39-f10c8aa3f931    /// </summary>    internal class Program    {        private static voID Main(string[] args)        {            string HTML =                new WebClIEnt().DownloadString(                    "https://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);            }        }    }}

结果:

<!DOCTYPE HTML PUBliC "-//W3C//DTD xhtml 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd"><HTML xmlns="http://www.w3.org/1999/xhtml">  <head>    <Meta name="generator" content="HTML TIDy for windows (vers 14 October 2008),see www.w3.org" />    <Title>      HTML Agility Pack: make code look neat - Stack Overflow    </Title>    <link rel="stylesheet" href="http://sstatic.net/so/all.CSS?v=6638" type="text/CSS" />    <link rel="shortcut icon" href="http://sstatic.net/so/favicon.ico" />    <link rel="apple-touch-icon" href="http://sstatic.net/so/apple-touch-icon.png" />    <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href=    "http://sstatic.net/so/opensearch.xml" />    <script type="text/JavaScript" src="http://AJAX.GoogleAPIs.com/AJAX/libs/jquery/1.3.2/jquery.min.Js"></script>    <script type="text/JavaScript" src="http://sstatic.net/so/Js/master.Js?v=6523"></script>    <script type="text/JavaScript">//<![cdaTA[    var imagePath='http://sstatic.net/so/img/';    //]]>    </script>    <link rel="canonical" href="https://stackoverflow.com/questions/2593147/HTML-agility-pack-make-code-look-neat" />    <link rel="alternate" type="application/atom+xml" Title=    "Feed for question 'HTML Agility Pack: make code look neat'" href="/Feeds/question/2593147" />    <script src="http://sstatic.net/so/Js/question.Js?v=6714" type="text/JavaScript"></script>    <script type="text/JavaScript">//<![cdaTA[        var fkey = "b00609a1a5f2966a687eca3f84e4dd64";        $(function() {            Vote.init(2593147);            comments.init();            styleCode();        });    //]]>    </script>  </head>  <body>    <noscript>    <div ID="noscript-padding"></div></noscript>    <div ID="notify-container"></div><script type="text/JavaScript">//<![cdaTA[        $(function() { notify.showFirstTime(); });    //]]>    </script>    <div >      <div ID="header">        <div ID="topbar">          <div ID="hlinks">            <a href=            "/users/login?returnurl=%2fquestions%2f2593147%2fHTML-agility-pack-make-code-look-neat%2f2610903">login</a>            <span >|</span> <a href="http://careers.stackoverflow.com/">careers</a> <span class=            "lsep">|</span> <a href="/about">about</a> <span >|</span> <a href="/faq">faq</a>          </div>          <div ID="hsearch">            <form ID="search" action="/search" method="get" name="search">              <div>                <input name="q"  tabindex="1" onfocus="if (this.value=='search') this.value = ''" type=                "text" maxlength="80" size="28" value="search" />              </div>            </form>          </div>        </div><br  />        <div ID="hlogo">          <a href="/"><img src="http://sstatic.net/so/img/logo.png" wIDth="250" height="61" alt="Stack Overflow" /></a>        </div>        <div ID="hmenus">          <div >            <ul>              <li >                <a href="/questions">Questions</a>              </li>              <li>                <a href="/Tags">Tags</a>              </li>              <li>                <a href="/users">Users</a>              </li>              <li>                <a href="/badges">Badges</a>              </li>              <li>                <a href="/unanswered">Unanswered</a>              </li>            </ul>          </div>          <div  >            <ul>              <li >                <a href="/questions/ask">Ask Question</a>              </li>            </ul>          </div>        </div>      </div>      <div ID="content">        <div ID="question-header">          <h2>            <a href="/questions/2593147/HTML-agility-pack-make-code-look-neat" >HTML Agility            Pack: make code look neat</a>          </h2>        </div>        <div ID="mainbar">          <div ID="question" >            <div >              <script type="text/JavaScript">//<![cdaTA[              document.write('<s'+'cript lang' + 'uage="jav' + 'ascript" src="http://ads.stackoverflow.com/a.aspx?ZoneID=3&amp;Task=Get&amp;IFR=False&amp;PageID=52405&amp;SiteID=1&amp;Random=' + (+new Date()) + '&amp;Keywords=HTMLagilitypack">');               document.write('</'+'scr'+'ipt>');              //]]>              </script> <noscript>              <div>                <a href=                "http://ads.stackoverflow.com/a.aspx?ZoneID=3&amp;Task=Click&amp;Mode=HTML&amp;SiteID=1&amp;PageID=52405">                <img src=                "http://ads.stackoverflow.com/a.aspx?ZoneID=3&amp;Task=Get&amp;Mode=HTML&amp;SiteID=1&amp;PageID=52405"                alt="" /></a>              </div></noscript>            </div>            <table>              <tr>                <td >                  <div >                    <input type="hIDden" value="2593147" /> <img  src=                    "http://sstatic.net/so/img/Vote-arrow-up.png" wIDth="40" height="25" alt="Vote up" Title=                    "This question is useful and clear (click again to undo)" /> <span >1</span>                    <img  src="http://sstatic.net/so/img/Vote-arrow-down.png" wIDth="40" height="25"                    alt="Vote down" title="This question is unclear or not useful (click again to undo)" /> <img class=                    "Vote-favorite" src="http://sstatic.net/so/img/Vote-favorite-off.png" wIDth="32" height="31" alt=                    "star" title="This is a favorite question (click again to undo)" />                    <div ></div>                  </div>                </td>                <td>                  <div>                    <div >                      <p>                        Can I use HTML Agility Pack to make the output look nicely indented,unnecessary white space                        stripped?                      </p>                    </div>                    <div >                      <a href="/questions/tagged/HTMLagilitypack"  Title=                      "show questions tagged 'HTMLagilitypack'" rel="tag">HTMLagilitypack</a>                    </div>                    <table >                      <tr>                        <td >                          <div >                            <a ID="flag-post-2593147" title="flag this post for serIoUs problems" name=                            "flag-post-2593147">flag</a>                          </div>                        </td>                        <td >                          <div >                            <div >                              asked <span title="2010-04-07 14:13:47Z" >2 days ago</span>                            </div>                            <div >                              <a href="/users/51795/illdev"><img src=                              "http://www.gravatar.com/avatar/52dc0db2cdacc6e9769d074a37466317?s=32&amp;d=IDenticon&amp;r=PG"                              height="32" wIDth="32" alt="" /></a>                            </div>                            <div >                              <a href="/users/51795/illdev">illdev</a><br />                              <span  title="reputation score">53</span><span Title=                              "5 bronze badges"><span >&#9679;</span><span class=                              "badgecount">5</span></span>                            </div>                          </div><br  />                          <div  Title=                          "this user has accepted an answer for 2 of 4 eligible questions">                            50% accept rate                          </div>                        </td>                      </tr>                    </table>                  </div>                </td>              </tr>              <tr>                <td ></td>                <td>                  <div ID="comments-2593147" >                    <table>                      <tbody>                        <tr ID="comment-2600849" >                          <td></td>                          <td >                            <div>                              what output? From where? some more details perhaps? &ndash;&nbsp;<a href=                              "/users/97614/sam-holder" title="1868" >Sam Holder</a> <span class=                              "comment-date"><span title="2010-04-07 14:16:41Z">2 days ago</span></span>                            </div>                          </td>                        </tr>                        <tr ID="comment-2600851" >                          <td></td>                          <td >                            <div>                              <i>(reference)</i> <a href="http://HTMLagilitypack.codeplex.com/Wikipage" rel=                              "nofollow">HTMLagilitypack.codeplex.com/Wikipage</a> &ndash;&nbsp;<a href=                              "/users/208809/gordon" title="16497" >Gordon</a> <span class=                              "comment-date"><span title="2010-04-07 14:16:55Z">2 days ago</span></span>                            </div>                          </td>                        </tr>                        <tr ID="comment-2624419" >                          <td></td>                          <td >                            <div>                              output = HTML code output &ndash;&nbsp;<a href="/users/51795/illdev" title="53" class=                              "comment-user owner">illdev</a> <span ><span Title=                              "2010-04-10 13:14:42Z">12 secs ago</span></span>                            </div>                          </td>                        </tr>                      </tbody>                    </table>                  </div>                </td>              </tr>            </table>          </div>          <div ID="answers">            <a name="tab-top" ID="tab-top"></a>            <div ID="answers-header">              <div ID="subheader">                <h2>                  2 Answers                </h2>                <div ID="tabs">                  <a href="/questions/2593147?tab=oldest#tab-top" Title=                  "Answers in the order they were given">oldest</a> <a href="/questions/2593147?tab=newest#tab-top"                  title="Most recent answers first">newest</a> <a  href=                  "/questions/2593147?tab=Votes#tab-top" title="Answers with the most Votes first">Votes</a>                </div>              </div>            </div><a name="2610845"></a>            <div ID="answer-2610845" >              <table>                <tr>                  <td >                    <div >                      <input type="hIDden" value="2610845" /> <img  src=                      "http://sstatic.net/so/img/Vote-arrow-up.png" wIDth="40" height="25" alt="Vote up" Title=                      "This answer is useful (click again to undo)" /> <span >0</span>                      <img  src="http://sstatic.net/so/img/Vote-arrow-down.png" wIDth="40" height="25"                      alt="Vote down" title="This answer is not useful (click again to undo)" />                    </div>                  </td>                  <td>                    <div >                      <p>                        A variation of this question has been answered recently                      </p>                      <ul>                        <li>                          <a href=                          "https://stackoverflow.com/questions/2490765/which-is-the-best-HTML-tIDy-pack-is-there-any-option-in-HTML-agility-pack-to-mak/2507673#2507673">                          https://stackoverflow.com/questions/2490765/which-is-the-best-HTML-tIDy-pack-is-there-any-option-in-HTML-agility-pack-to-mak/2507673#2507673</a>                        </li>                      </ul>                      <p>                        Basically the outcome of this was that while you <strong>can</strong> use HTMLAgilityPack to                        clean it up a bit by using the fix nested Tags.                      </p>                      <p>                        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.                      </p>                      <p>                        This article covers how to use it but you had to sign up (free) to vIEw it:                      </p>                      <ul>                        <li>                          <a href="http://www.devx.com/dotnet/Article/20505/1763/" rel=                          "nofollow">http://www.devx.com/dotnet/Article/20505/1763/</a>                        </li>                      </ul>                      <p>                        It seems like a legit article but its funny because nobody else seems to have covered this                        topic in the last six years...                      </p>                    </div>                    <table >                      <tr>                        <td >                          <div >                            <a href="/questions/2593147/HTML-agility-pack-make-code-look-neat/2610845#2610845" Title=                            "permalink to this answer">link</a><span >|</span><a ID="flag-post-2610845"                            title="flag this post for serIoUs problems" name="flag-post-2610845">flag</a>                          </div>                        </td>                        <td align="right" >                          <div >                            <div >                              answered <span title="2010-04-09 20:55:18Z" >16 hours ago</span>                            </div>                            <div >                              <a href="/users/156388/rtpharry"><img src=                              "http://www.gravatar.com/avatar/6811db2b37e824fdf6c5c4fcdddd4146?s=32&amp;d=IDenticon&amp;r=PG"                              height="32" wIDth="32" alt="" /></a>                            </div>                            <div >                              <a href="/users/156388/rtpharry">rtpHarry</a><br />                              <span  title="reputation score">88</span><span Title=                              "6 bronze badges"><span >&#9679;</span><span class=                              "badgecount">6</span></span>                            </div>                          </div>                        </td>                      </tr>                    </table>                  </td>                </tr>                <tr>                  <td ></td>                  <td>                    <div ID="comments-2610845" >                      <table>                        <tbody>                          <tr>                            <td></td>                            <td></td>                          </tr>                        </tbody>                      </table>                    </div>                  </td>                </tr>              </table>            </div>            <div >              <script type="text/JavaScript">//<![cdaTA[              document.write('<s'+'cript lang' + 'uage="jav' + 'ascript" src="http://ads.stackoverflow.com/a.aspx?ZoneID=14&amp;Task=Get&amp;IFR=False&amp;PageID=52405&amp;SiteID=1&amp;Random=' + (+new Date()) + '&amp;Keywords=HTMLagilitypack">');               document.write('</'+'scr'+'ipt>');              //]]>              </script> <noscript>              <div>                <a href=                "http://ads.stackoverflow.com/a.aspx?ZoneID=14&amp;Task=Click&amp;Mode=HTML&amp;SiteID=1&amp;PageID=52405">                <img src=                "http://ads.stackoverflow.com/a.aspx?ZoneID=14&amp;Task=Get&amp;Mode=HTML&amp;SiteID=1&amp;PageID=52405"                alt="" /></a>              </div></noscript>            </div><a name="2610903"></a>            <div ID="answer-2610903" >              <table>                <tr>                  <td >                    <div >                      <input type="hIDden" value="2610903" /> <img  src=                      "http://sstatic.net/so/img/Vote-arrow-up.png" wIDth="40" height="25" alt="Vote up" Title=                      "This answer is useful (click again to undo)" /> <span >0</span>                      <img  src="http://sstatic.net/so/img/Vote-arrow-down.png" wIDth="40" height="25"                      alt="Vote down" title="This answer is not useful (click again to undo)" />                    </div>                  </td>                  <td>                    <div >                      <p>                        Output as xhtml and run that through an <a href=                        "http://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.indentation.aspx" rel=                        "nofollow">XmlTextWriter</a>                      </p>                    </div>                    <table >                      <tr>                        <td >                          <div >                            <a href="/questions/2593147/HTML-agility-pack-make-code-look-neat/2610903#2610903" Title=                            "permalink to this answer">link</a><span >|</span><a ID="flag-post-2610903"                            title="flag this post for serIoUs problems" name="flag-post-2610903">flag</a>                          </div>                        </td>                        <td align="right" >                          <div >                            <div >                              answered <span title="2010-04-09 21:02:34Z" >16 hours ago</span>                            </div>                            <div >                              <a href="/users/242897/sky-sanders"><img src=                              "http://www.gravatar.com/avatar/df4a7fbd8a054fd6193ca0ee62952f1f?s=32&amp;d=IDenticon&amp;r=PG"                              height="32" wIDth="32" alt="" /></a>                            </div>                            <div >                              <a href="/users/242897/sky-sanders">Sky Sanders</a><br />                              <span  title="reputation score">4,014</span><span Title=                              "2 silver badges"><span >&#9679;</span><span class=                              "badgecount">2</span></span><span title="14 bronze badges"><span class=                              "badge3">&#9679;</span><span >14</span></span>                            </div>                          </div>                        </td>                      </tr>                    </table>                  </td>                </tr>                <tr>                  <td ></td>                  <td>                    <div ID="comments-2610903" >                      <table>                        <tbody>                          <tr>                            <td></td>                            <td></td>                          </tr>                        </tbody>                      </table>                    </div>                  </td>                </tr>              </table>            </div>            <form ID="post-form" action="/questions/2593147/answer/submit" method="post" name="post-form">              <h2 >                Your Answer              </h2><script src="http://sstatic.net/so/Js/wmd.Js?v=6016" type="text/JavaScript"></script> <script type="text/JavaScript">//<![cdaTA[              $(function() {               editorReady(1,heartbeat.answers);              });                 //]]>              </script>              <div ID="post-editor">                <div ID="wmd-container">                  <div ID="wmd-button-bar"></div>                  <textarea ID="wmd-input" name="post-text" cols="92" rows="15" tabindex="101"></textarea>                </div>                <div >                  <input ID="communitymode" name="communitymode" type="checkBox" /> <label for="communitymode" Title=                  "community owned posts do not generate any reputation for the owner,have a lower reputation barrIEr for collaborative editing,and show only a revision history instead of a signature block">                  community wiki</label>                </div>                <div ID="wmd-prevIEw"></div>                <div ID="edit-block">                  <input ID="fkey" name="fkey" type="hIDden" value="b00609a1a5f2966a687eca3f84e4dd64" /> <input ID=                  "author" name="author" type="text" />                </div>              </div>              <div >                <table>                  <tr>                    <td >                      <label for="openID_IDentifIEr">OpenID Login</label> <input ID="openID_IDentifIEr" name=                      "openID_IDentifIEr"  type="text" size="40" maxlength="200" value=""                      tabindex="104" />                      <div >                        Get an <a href="http://openID.net/get/" target="_blank">OpenID</a>                      </div>                    </td>                    <td >                      <div >                        or                      </div>                      <div ></div>                    </td>                    <td >                      <div>                        <label for="display-name">name</label> <input ID="display-name" name="display-name" type="text"                        size="30" maxlength="30" value="" tabindex="105" />                      </div>                      <div>                        <label for="m-address">Email</label> <input ID="m-address" name="m-address" type="text" size=                        "40" maxlength="100" value="" tabindex="106" /> <span  style=                        "color:#999; Font-weight:normal">never shown</span>                      </div>                      <div>                        <label for="home-page">Home Page</label> <input ID="home-page" name="home-page" type="text"                        size="40" maxlength="200" value="" tabindex="107" />                      </div>                    </td>                  </tr>                </table>              </div>              <div >                <input ID="submit-button" type="submit" value="Post Your Answer" tabindex="110" />              </div>            </form>            <h2 >              Not the answer you're looking for? browse other questions tagged <a href=              "/questions/tagged/HTMLagilitypack"  title="show questions tagged 'HTMLagilitypack'" rel=              "tag">HTMLagilitypack</a> or <a href="/questions/ask">ask your own question</a>.            </h2>          </div><img src="/posts/2593147/ivc/1707"  alt="" />        </div>
总结

以上是内存溢出为你收集整理的html-agility-pack – Html Agility Pack:使代码看起来整洁全部内容,希望文章能够帮你解决html-agility-pack – Html Agility Pack:使代码看起来整洁所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1133498.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存