This is my HTML code. How to select first link(Main link 1, Main link 2,...) after <li id="item1"> ,<li id="item2"> and etc.
<div id="mainmenu">
<div class="wrapper">
<div class="main-menu">
<ul class="navigation">
<li class="menu-group">
<ul>
<li id="item1">
Main link 1
<div id="item1-sub">
<ul>
<li>
subLink1
</li>
<li>
subLink2
</li>
</ul>
</div>
</li>
<li id="item2">
Main link 2
<div id="item2-sub">
<ul>
<li>
subLink1
</li>
<li>
subLink2
</li>
</ul>
</div>
</li>
</ul>
</li>
</ul>
</div>
</div>
C# code:
This code has problems. I hope someone can help to solve problems.
var webGet = new HtmlWeb();
var document = webGet.Load("file.html");
var menuGroup = document.DocumentNode.SelectNodes("//div[#id='mainmenu']//div[#class='wrapper']//div[#class='main-menu']//ul[#class='navigation']//li[#class='menu-group']//ul//li");
if (menuGroup != null)
{
foreach (var Tag in menuGroup)
{
var atag = Tag.SelectSingleNode("./a");
}
}
You could do this
var html = "<html><head></head><body><div id=\"mainmenu\"><div class=\"wrapper\"><div class=\"main-menu\"><ul class=\"navigation\"><li class=\"menu-group\"><ul><li id=\"item1\">" +
"Main link 1<div id=\"item1-sub\"><ul><li>subLink1</li><li>subLink2</li></ul></div></li><li id=\"item2\">" +
"Main link 2<div id=\"item2-sub\"><ul><li>subLink1</li><li>subLink2</li></ul></div></li></ul></li></ul></div></div></div></body></html>";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var mainLink1 = doc.DocumentNode.SelectSingleNode("//li[#id='item1']//a");
var linkUrl1 = mainLink1.Attributes["href"].Value;
var linkText1 = mainLink1.InnerText;
var mainLink2 = doc.DocumentNode.SelectSingleNode("//li[#id='item2']//a");
var linkUrl2 = mainLink2.Attributes["href"].Value;
var linkText2 = mainLink2.InnerText;
OUTPUT:
EDIT:
Use the following code to get main links with loop, This will pick only your desired links.
foreach (var div in doc.DocumentNode.SelectNodes("//li[#class='menu-group']//ul//li//div"))
{
div.InnerHtml = string.Empty;
}
foreach (var a in doc.DocumentNode.SelectNodes("//li[#class='menu-group']//ul//li//a"))
{
var linkUrl = a.Attributes["href"].Value;
var linkText = a.InnerText;
}
Related
I am trying to convert from html to .txt with HTML AGILITY PACK.
I would like to retain the numberings or letterings from the ordered or unordered list to keep the same rendering.
Given a HTML like :
<h1> Title </h1>
<h2> Sub Title </h2>
<ol>
<li style="list-style-type: lower-alpha;"> First element </li>
<li style="list-style-type: lower-alpha;"> Second element </li>
<li style="list-style-type: lower-alpha;"> Third element </li>
</ol>
<h3> Diferent title</h3>
<ol>
<li style="list-style-type: lower-roman;"> First element </li>
<li style="list-style-type: lower-roman;"> Second element </li>
</ol>
<h3> Diferent title</h3>
<p> Some text </p>
<ol>
<li> First element </li>
<li> Second element </li>
</ol>
<h3> Unordered list title</h3>
<ul>
<li> First element </li>
<li> Second element </li>
</ul>
I cannot find the way. I tried with this:
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");
var lists = htmlDoc.DocumentNode.SelectNodes("//ol"); //todas las <ol>
foreach (HtmlNode node in lists)
{
var elementInList = node.SelectNodes(".//li").Count;
Console.WriteLine("elementInList "+ elementInList);
for ( int i=0; i < elementInList;i++){
Console.WriteLine( i+1 + ". "+ node.SelectNodes(".//li")[i].InnerHtml);
}
But I do want it to appear in the text like that, to substitute the < li > in order to have the position like " 1. First Element" (not in the console)
Thank you in advance
I used this code with bootstrap 3, and it worked correctly, when clicking on the page, it appeared as active, but when switching to bootstrap 4, it no longer calls the active class, when I pass the page number inside the "for"
Any ideias how to resolve that?
<ul class="pagination">
<li class="page-item ">
#{
if (ViewBag.pageNumber> 1)
{
<a class="page-link" href="#Url.Action("Index", "Barcos", new { search= ViewBag.Search, pageNumber= ViewBag.pageNumber- 1 })">before</a>
}
else
{
<a class="page-link" href="#">
before
</a>
}
}
</li>
#{
var page = ViewBag.pageNumber;
for (var i = 1; i <= ViewBag.Count; i++)
{
<li #(page== i ? "class=page-item active" : "")>
<a class="page-link" href="#Url.Action("Index", "Barcos", new {search= ViewBag.Search, pageNumber= i})">#i</a>
</li>
}
}
<li>
#if (ViewBag.pageNumber< ViewBag.count)
{
<a class="page-link" href="#Url.Action("Index", "Barcos", new { Search= ViewBag.Search, pageNumber= ViewBag.pageNumber+ 1 })">Next</a>
}
else
{
<a class="page-link disabled" href="#">
Before
</a>
}
</li>
</ul>
You need class="page-item" on all list items, not just for your current page
Corrected bode below.
#for (var i = 1; i <= ViewBag.Count; i++)
{
var page = ViewBag.pageNumber;
string classes = "page-item";
if (page == i)
{
classes += " active";
}
<li class="#classes">
#Html.ActionLink(i.ToString(), "Index", "Barcos", new { search = ViewBag.Search, pageNumber = i }, new { #class = "page-link" })
</li>
}
This generates the following HTML
<ul class="pagination">
<li class="page-item ">
<a class="page-link" href="/Barcos/Index?pageNumber=1">before</a>
</li>
<li class="page-item">
<a class="page-link" href="/Barcos/Index?pageNumber=1">1</a>
</li>
<li class="page-item active">
<a class="page-link" href="/Barcos/Index?pageNumber=2">2</a>
</li>
<li class="page-item">
<a class="page-link" href="/Barcos/Index?pageNumber=3">3</a>
</li>
<li>
<a class="page-link" href="/Barcos/Index?pageNumber=4">Next</a>
</li>
</ul>
I has a problem to creating HTML-list base on array in c#.
I tried using split.string, foreach, and etc,. but still can't figured out the logic... :(..
Anyone can help me to solve my problem ?
Here is my Array
List<string> listMenu = new List<string>();
listMenu.Add("Dashboard~View1");
listMenu.Add("Dashboard~View2");
listMenu.Add("Customer");
listMenu.Add("Part");
listMenu.Add("Part~Part1~Part11");
listMenu.Add("Part~Part1~Part12");
listMenu.Add("Part~Part2~Part21");
listMenu.Add("Part~Part2~Part22");
listMenu.Add("Part~Part3~Part31~Part311");
listMenu.Add("Part~Part3~Part31~Part312");
listMenu.Add("Branch");
And I want to create HTML list like this :
<div id=menu>
<ul>
<li>Dahboard
<ul>
<li> View1 </li>
<li> View2 </li>
</ul>
</li>
<li> Customer
</li>
<li> Part
<ul>
<li> Part1
<ul>
<li> Part11
</li>
<li> Part12
</li>
</ul>
</li>
<li> Part2
<ul>
<li> Part21
</li>
<li> Part22
</li>
</ul>
</li>
<li> Part3
<ul>
<li> Part31
<ul>
<li> Part 311
</li>
</ul>
</li>
<li> Part 312
</li>
</ul>
</li>
</ul>
</li>
<li> Branch
</li>
</ul>
</div>
There are two ways you can use to create this list dynamically:
You can use HtmlGenericControl structure as below:
HtmlGenericControl c = new HtmlGenericControl("div");
c.Attributes.Add("id", "menu");
HtmlGenericControl ul1 = new HtmlGenericControl("ul");
c.Controls.Add(ul1);
HtmlGenericControl li1 = new HtmlGenericControl("li");
li1.InnerText = "Dashboard";
ul1.Controls.Add(li1);
mainDiv.Controls.Add(c);
You can create a string with StringBuilder and assign this string as InnerHtml to your main div, as below:
StringBuilder s = new StringBuilder();
s.Append(#"<div id=menu>
<ul>
<li>Dahboard
<ul>
<li> View1 </li>
<li> View2 </li>
</ul>
</li>
</ul>
</div> "); //you can alter this string dynamically
mainDiv.InnerHtml = s.ToString();
You should add System.Web.UI.HtmlControls as using to use
HtmlControls
You should add System.Text as using in order to use StringBuilder.
Bonus: You can find from here why you should use StringBuilder
instead of adding string with '+'.
I have the following HTML
<div id='cssmenu'>
<ul>
<li class= 'has-sub'>
<a href=store.aspx?id=dLYTWvt8EsHOq7Ps2wJA9A%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat</span></a>
<ul>
<li class= 'has-sub'>
<a href=store.aspx?id=xEDEzZWDRkX8%2fbXoMX2pSQ%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-2</span></a>
<ul>
<li class= 'active'><a href=store.aspx?id=TxlSRrnSZ6HDgj%2b2ZSxRhg%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Dance -1</span></a></li>
<li class= 'active'><a href=store.aspx?id=Y7JxNAXSuG1T%2f0cpjQXSwA%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Dance - 2</span></a></li>
<li class= 'active'><a href=store.aspx?id=%2fcoJMcJTjWp3%2bPuimR5AhA%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-5</span></a></li>
</ul>
</li>
<li class= 'has-sub'>
<a href=store.aspx?id=KC2g4igBzXisbsbKu%2fKrzw%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-3</span></a>
<ul>
<li class= 'active'><a href=store.aspx?id=8DCHnP%2b4KQVYDTGxVt9snQ%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Death -1</span></a></li>
<li class= 'active'><a href=store.aspx?id=nGEm4rbNMQ%2bQfyO44ECmpA%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Death - 1</span></a></li>
</ul>
</li>
<li class= 'has-sub'><a href=store.aspx?id=feki%2bXDs66obnO1e2dxHWg%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-4</span></a></li>
<li class= 'has-sub'><a href=store.aspx?id=5O394Lww9oxD1vHbIY7LWw%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-6</span></a></li>
</ul>
</li>
<li class= 'active'><a href=store.aspx?id=RIe63RBggHzj76SUwDHKMg%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Dance</span></a></li>
</ul>
</div>
And in reqid
var reqid = "<%=Request["id"]%>";
In reqid I will get urlencoded based on menu selection
My jQuery code here
$(function () {
var str = $("#cssmenu").find('li').find('a').attr('href');
if ($(str).has(reqid)) {
var str2 = $("#cssmenu").find('li').find('a').text();
$('#selectedmenuitem').html(str2);
}
});
If the href attribute encoded url contains selected menu item encodedurl i want to get the matched url .text() to the #selectedmenuitem label id.
I checked with contains, am not getting. Please suggest me what are other possibilities for this.
use indexOf function of Javascript
http://www.w3schools.com/jsref/jsref_indexof.asp
Use following jquery code
$(function () {
var str2= $("#cssmenu").find("li").find("a[href*='" + reqid + "']").text();
$('#selectedmenuitem').html(str2);
});
I got a strange errror. My html helper is supose to render html for a menu, simple ul's and li's. now the problem becomes that it starts to render a tags around other elements. The string returned from the html is correct, it seems something after that is causing the problem. I just cant figure out why. So i thought maybe a fresh pair of eys could spot it.
method
public static MvcHtmlString TopMenuLinks(this HtmlHelper helper, List<IMenuItemNodeViewData> menuItem)
{
if (menuItem != null && menuItem.Count > 0)
{
var count = 0;
var resultString = "";
var ulTag = new TagBuilder("ul");
foreach (var item in menuItem.Where(x => x.IsVisibleInMenu))
{
count++;
var liTag = new TagBuilder("li")
{
InnerHtml = string.Format("<a href=\"{0}\"><img src=\"{1}\" alt=\"{1}\" />{2}", item.URL, "/Content/images/image.png", item.Name)
};
ulTag.InnerHtml += liTag.ToString();
if (count % 3 == 0 || count == menuItem.Where(x => x.IsVisibleInMenu).Count())
{
resultString += ulTag.ToString(TagRenderMode.Normal);
ulTag = new TagBuilder("ul");
}
}
return MvcHtmlString.Create(resultString);
}
return null;
}
partialview
<div class="top-columns link-container">
<div class="links">
<%= Html.TopMenuLinks(Model.MenuItems) %>
</div>
</div>
<div class="top-columns icons">
<div class="icons-contain">
<img src="/Content/images/image.png" alt="image" />
</div>
</div>
It renders the following
<div class="links">
<ul>
<li>..</li>
<li>..</li>
<li>..</li>
</ul>
<ul>
<li>..</li>
<li>..</li>
</ul>
</div>
</div>
<a href="url">
<div class="top-columns icons">
<div class="icons-contain">
</div>
</a>
</div>
It suppose to be
<div class="links">
<ul>
<li>..</li>
<li>..</li>
<li>..</li>
</ul>
<ul>
<li>..</li>
<li>..</li>
</ul>
</div>
<div class="top-columns icons">
<div class="icons-contain">
</div>
</div>
you forgot to close <a> tag in the following line :
InnerHtml = string.Format("<a href=\"{0}\"><img src=\"{1}\" alt=\"{1}\" />{2}", item.URL, "/Content/images/image.png", item.Name)
try this :
InnerHtml = string.Format("<img src=\"{1}\" alt=\"{1}\" />{2} ", item.URL, "/Content/images/image.png", item.Name)