Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursively create accordion html from list of links in C#
    primarykey
    data
    text
    <p>I am scraping a remote page for links, which I have in a List. I am trying to get the list printed out as necessary for a jquery accordion. I have the html no problem, but my loops are off a bit and aren't nesting properly. </p> <p>Here is an example of the List:</p> <pre><code>&lt;a href="#heading1"&gt;Heading 1&lt;/a&gt; &lt;a href="link1.html"&gt;link1&lt;/a&gt; &lt;a href="link2.html"&gt;link2&lt;/a&gt; &lt;a href="link3.html"&gt;link3&lt;/a&gt; &lt;a href="#heading2"&gt;Heading 2&lt;/a&gt; &lt;a href="link4.html"&gt;link4&lt;/a&gt; &lt;a href="link5.html"&gt;link5&lt;/a&gt; &lt;a href="link6.html"&gt;link6&lt;/a&gt; </code></pre> <p>So here is how I need to get the html formatted:</p> <pre><code>&lt;div data-role="collapsible" data-collapsed="true"&gt; &lt;h3&gt;Heading 1&lt;/h3&gt; &lt;p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="link1.html"&gt;link1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="link2.html"&gt;link2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="link3.html"&gt;link3&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/p&gt; &lt;/div&gt; &lt;div data-role="collapsible" data-collapsed="true"&gt; &lt;h3&gt;Heading 2&lt;/h3&gt; &lt;p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="link4.html"&gt;link4&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="link5.html"&gt;link5&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="link6.html"&gt;link6&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/p&gt; &lt;/div&gt; </code></pre> <p>And here is the code I have so far, it should be mentioned I am using the HTMLAgilityPack:</p> <pre><code>HtmlNodeCollection OneHome = document.DocumentNode.SelectNodes("//div[@id='accordion1']"); var OneHomelinks = OneHome.Descendants("a") .Select(a =&gt; a.OuterHtml) .ToList(); foreach (string link in OneHomelinks) { if (link.Contains('#')) { Response.Write("&lt;div data-role=\"collapsible\" data-collapsed=\"true\"&gt;"); Response.Write("&lt;h3&gt;" + link + "&lt;/h3&gt;"); Response.Write("&lt;p&gt;"); Response.Write("&lt;ul&gt;"); } if (!link.Contains('#')) { Response.Write("&lt;li&gt;" + link + "&lt;/li&gt;"); } else { Response.Write("&lt;/ul&gt;"); Response.Write("&lt;/p&gt;"); Response.Write("&lt;/div&gt;"); } } </code></pre> <p>Basically I am trying to pull out the Headings by using the '#' in the href, and resetting the at that point - but something is off, the lists aren't being properly formed. Any help is super appreciated here!</p> <p><strong>CURRENT OUTPUT</strong></p> <pre><code>&lt;div data-role="collapsible" data-collapsed="true"&gt; &lt;h3&gt;Heading 1&lt;/h3&gt; &lt;p&gt;&lt;/p&gt; &lt;ul&gt;&lt;/ul&gt; &lt;p&gt;&lt;/p&gt; &lt;/div&gt; &lt;li&gt;&lt;a href="link1.html&gt;link1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="link2.html&gt;link2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="link3.html&gt;link3&lt;/a&gt;&lt;/li&gt; &lt;div data-role="collapsible" data-collapsed="true"&gt; &lt;h3&gt;Heading 2&lt;/h3&gt; &lt;p&gt;&lt;/p&gt; &lt;ul&gt;&lt;/ul&gt; &lt;p&gt;&lt;/p&gt; &lt;/div&gt; &lt;li&gt;&lt;a href="link4.html&gt;link4&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="link5.html&gt;link5&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="link6.html&gt;link6&lt;/a&gt;&lt;/li&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload