I want a regex solution to find some text value which looks like MLA818214667 and this value placed in a id like id="MLA818214667". There should be 3 type of pattern to find these value from HTML.
It should start with MLA and placed in id="".
The number after MLA should be more than 6 characters long.
The number should be fully numeric not string mixed.
Note: I want to avoid HtmlAgilityPack for this case because the text not always valid html. So i want to treat it as text not html and need solution without any html parser
C#:
var listOfIds = new List<string>();
string html = #"below html sample goes here";
Match match = Regex.Match(input, #"/([A-Za-z0-9\-]+)\.$",
RegexOptions.IgnoreCase);
//from matched ids it should be added in list listOfIds
Html:
<span class="main-title">
Casco Integral Halcon H57 + Combo Termico Invierno Sti Motos
</span>
</h2>
<div class="item__status">
<div class="item__condition">541 vendidos</div>
</div>
</div>
</a>
<form class="item__bookmark-form" action="/search/bookmarks/MLA614364106/make" method="post" id="bookmarkForm" class="bookmark-form">
<button type="submit" class="bookmarks favorite" data-id="MLA614364106">
<div class="item__bookmark">
<div class="icon"></div>
</div>
</button>
<input type="hidden" name="method" value='add'/>
<input type="hidden" name="itemId" value='MLA614364106'/>
<input type="hidden" name="_csrf" value="5fe7b4e6-19d3-42bc-a3bb-15eaeee81f64"/>
</form>
</div>
</li>
<li class="results-item highlighted article grid item-info-height-179">
<div class="rowItem item highlighted item--grid item--has-row-logo new" id="MLA751765547">
<div class="item__image item__image--grid">
<div class="images-viewer" item-url="https://articulo.mercadolibre.com.ar/MLA-751765547-casco-moto-hawk-htl-dr46-rebatible-lett-store-_JM#position=5&type=item&tracking_id=897c653e-1565-4371-8a4d-b2ea29d09d4d" item-id="MLA751765547">
<div class="carousel">
<ul>
<li><a href="https://articulo.mercadolibre.com.ar/MLA-751765547-casco-moto-hawk-htl-dr46-rebatible-lett-store-_JM#position=5&type=item&tracking_id=897c653e-1565-4371-8a4d-b2ea29d09d4d" class="item-link item__js-link">
<img class='lazy-load' width='284' height='284' alt='Casco Moto Hawk Htl Dr46 Rebatible Lett Store' src='https://http2.mlstatic.com/casco-moto-hawk-htl-dr46-rebatible-lett-store-D_NQ_NP_624166-MLA31021954439_062019-W.jpg'/>
</a>
</li>
</ul>
</div>
</div>
</div>
<span class="item-loading__status-bar item-loading__hide"></span>
<a href="https://articulo.mercadolibre.com.ar/MLA-751765547-casco-moto-hawk-htl-dr46-rebatible-lett-store-_JM#position=5&type=item&tracking_id=897c653e-1565-4371-8a4d-b2ea29d09d4d" class="item__info-link item__js-link">
<div class="item__info ">
<div class="item__price ">
<span class="price__symbol">$</span>
<span class="price__fraction">3.725</span>
</div>
<span class="item-installments item__installments--show-card-icon highlighted free-interest item--has-shipping">
<span class="item-installments-text">Hasta 6 cuotas sin interés</span>
</span>
<div class="item__shipping-promise item__shipping highlighted free-shipping">
<span class="text-shipping next_day">Llega gratis el lunes</span>
</div>
<div class="item__brand-logo item__brand-img--ultra-wide">
<span class="item__brand-img-container">
<img src="https://http2.mlstatic.com/D_NQ_NP_796276-MLA31050681849_062019-T.jpg"/>
</span>
</div>
<h2 class="item__title list-view-item-title">
<span class="main-title">Casco Moto Hawk Htl Dr46 Rebatible Lett Store</span>
</h2>
<div class="item__status">
<div class="item__condition">362 vendidos</div>
</div>
</div>
</a>
<form class="item__bookmark-form" action="/search/bookmarks/MLA751765547/make" method="post" id="bookmarkForm" class="bookmark-form">
<button type="submit" class="bookmarks favorite" data-id="MLA751765547">
<div class="item__bookmark">
<div class="icon"></div>
</div>
</button>
<input type="hidden" name="method" value='add'/>
<input type="hidden" name="itemId" value='MLA751765547'/>
<input type="hidden" name="_csrf" value="5fe7b4e6-19d3-42bc-a3bb-15eaeee81f64"/>
</form>
</div>
</li>
<li class="results-item highlighted article grid item-info-height-179">
<div class="rowItem item highlighted item--grid item--has-row-logo new to-item" id="MLA817988063">
<div class="item__image item__image--grid">
<div class="images-viewer" item-url="https://articulo.mercadolibre.com.ar/MLA-817988063-cascos-motos-vega-vflow-motocross-mx-enduro-atv-acces-cam-_JM#position=6&type=item&tracking_id=897c653e-1565-4371-8a4d-b2ea29d09d4d" item-id="MLA817988063">
<div class="carousel">
<ul>
<li>
<a href="https://articulo.mercadolibre.com.ar/MLA-817988063-cascos-motos-vega-vflow-motocross-mx-enduro-atv-acces-cam-_JM#position=6&type=item&tracking_id=897c653e-1565-4371-8a4d-b2ea29d09d4d" class="item-link item__js-link">
<img class='lazy-load' width='284' height='284' alt='Cascos Motos Vega Vflow Motocross Mx Enduro Atv + Acces Cam' src='https://http2.mlstatic.com/cascos-motos-vega-vflow-motocross-mx-enduro-atv-acces-cam-D_NQ_NP_629038-MLA32405702773_102019-W.jpg' />
</a>
</li>
</ul>
</div>
You can use this example "id=\"(MLA[0-9]{6,})\"" to find all the values of id form HTML
Paste the RegEx in here https://regex101.com to see how it works
static void Main(string[] args)
{
var listOfIds = new List<string>();
string html = " id=\"MLA12334566\" id=\"MLA123354566\" id=\"MLA123346566\"";
Regex idRegex = new Regex("id=\"(MLA[0-9]{6,})\"");
var matches = idRegex.Matches(html);
foreach(var match in matches)
{
listOfIds.Add(match.ToString());
}
}
Related
This question already has answers here:
Razor comment syntax
(4 answers)
Closed 3 years ago.
I have mixed c# and html code in razor.
how can I comment the whole as a one comment, this is the code?
#if (Request.Cookies["area"] != null && Request.Cookies["area"].Value != "")
{
<div>
<div class="search-main">
<div><input type="text" class="search-control" placeholder="Search ads..." /></div>
<div class="search-control .search-control-border" style="cursor:pointer;overflow-y:visible;">
<a style="display:block; width:100%;height:100%" onclick="expandCategoryDropdown()"> <span class="category-dropdown-text" style="float: left;margin-top: 2%;margin-left: 0.2em;">#(Request.Cookies["city"] != null ? Request.Cookies["city"].Value.ToString() : "Select Location")</span><i class="glyphicon glyphicon-chevron-down icon category-dropdown-icon" style="float: right;margin-top: 3%;"></i></a>
<div class="searh-dropdown" style="z-index:1">
<div class="inner-dropdown">
<ul style="margin-left:-38px">
#*<li class="select-cat-li" style="list-style-type:none"><a class="select-cat" style="color:black" onclick="select_cat(this,'parent')">#ViewBag.area</a></li>*#
#{
List<ListHell.CODE.Locations> lst = (List<ListHell.CODE.Locations>)ViewBag.cities;
}
<li class="select-cat-li" style="list-style-type:none">
#ViewBag.areaStr
<ul style="margin-left:-40px;">
#foreach (ListHell.CODE.Locations l in lst)
{
<li class="select-cat-li" style="list-style-type:none"><a class="select-cat" style="margin-left:10px;color:black" onclick="select_cat(this,'child')">#l.city</a></li>
}
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="search-control .search-control-border"></div>
</div>
<div><input type="button" class="form-control" placeholder="Search" value="Search" style="margin-top: -2.5% !important;margin-left: 76%;;width:20%" /></div>
</div>
}
I dont want embed comment within comment. Just one commenting should do. Possible?
This should work, it's working in my client.
#{/*
#if (Request.Cookies["area"] != null && Request.Cookies["area"].Value != "")
{
<div>
<div class="search-main">
<div><input type="text" class="search-control" placeholder="Search ads..." /></div>
<div class="search-control .search-control-border" style="cursor:pointer;overflow-y:visible;">
<a style="display:block; width:100%;height:100%" onclick="expandCategoryDropdown()"> <span class="category-dropdown-text" style="float: left;margin-top: 2%;margin-left: 0.2em;">#(Request.Cookies["city"] != null ? Request.Cookies["city"].Value.ToString() : "Select Location")</span><i class="glyphicon glyphicon-chevron-down icon category-dropdown-icon" style="float: right;margin-top: 3%;"></i></a>
<div class="searh-dropdown" style="z-index:1">
<div class="inner-dropdown">
<ul style="margin-left:-38px">
#*<li class="select-cat-li" style="list-style-type:none"><a class="select-cat" style="color:black" onclick="select_cat(this,'parent')">#ViewBag.area</a></li>*#
#{
List<ListHell.CODE.Locations> lst = (List<ListHell.CODE.Locations>)ViewBag.cities;
}
<li class="select-cat-li" style="list-style-type:none">
#ViewBag.areaStr
<ul style="margin-left:-40px;">
#foreach (ListHell.CODE.Locations l in lst)
{
<li class="select-cat-li" style="list-style-type:none"><a class="select-cat" style="margin-left:10px;color:black" onclick="select_cat(this,'child')">#l.city</a></li>
}
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="search-control .search-control-border"></div>
</div>
*/}
Should be #* {code} * # for multiline.
alt. /* and */
You can also try ctrl + k + c if you are using Visual Studio
Example:
#*
#{
/* C# comment */
// Another C# comment
}
<!-- HTML comment -->
*#
Similar Post answer
that should work
#* #if (Request.Cookies["area"] != null && Request.Cookies["area"].Value != "")
{
<div>
<div class="search-main">
<div><input type="text" class="search-control" placeholder="Search ads..." /></div>
<div class="search-control .search-control-border" style="cursor:pointer;overflow-y:visible;">
<a style="display:block; width:100%;height:100%" onclick="expandCategoryDropdown()"> <span class="category-dropdown-text" style="float: left;margin-top: 2%;margin-left: 0.2em;">#(Request.Cookies["city"] != null ? Request.Cookies["city"].Value.ToString() : "Select Location")</span><i class="glyphicon glyphicon-chevron-down icon category-dropdown-icon" style="float: right;margin-top: 3%;"></i></a>
<div class="searh-dropdown" style="z-index:1">
<div class="inner-dropdown">
<ul style="margin-left:-38px">
<!-- <li class="select-cat-li" style="list-style-type:none"><a class="select-cat" style="color:black" onclick="select_cat(this,'parent')">#ViewBag.area</a></li> -->
#{
List<ListHell.CODE.Locations> lst = (List<ListHell.CODE.Locations>)ViewBag.cities;
}
<li class="select-cat-li" style="list-style-type:none">
#ViewBag.areaStr
<ul style="margin-left:-40px;">
#foreach (ListHell.CODE.Locations l in lst)
{
<li class="select-cat-li" style="list-style-type:none"><a class="select-cat" style="margin-left:10px;color:black" onclick="select_cat(this,'child')">#l.city</a></li>
}
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="search-control .search-control-border"></div>
</div>
<div><input type="button" class="form-control" placeholder="Search" value="Search" style="margin-top: -2.5% !important;margin-left: 76%;;width:20%" /></div>
</div>
} *#
For Visual Studio, use Ctrl+A, Ctrl+K, Ctrl+C.
For JetBrains, use Ctrl+A, Ctrl+/.
Here's my problem, I have a Book that contains List<Page> that contains List<Line> and I'm trying to create a view that will edit my book. This view contains all the lines from the book. I've made an MVVM for List<Page> that calls smaller MVVM for List<Line>. The problem is that MVVM is not counting all Line as one single form, so this happens:
<form>
<h1>Page one</h1>
<input type="hidden" name="[0].lineContent" value=""/>
<input type="hidden" name="[1].lineContent" value=""/>
<h1>Page one</h1>
<input type="hidden" name="[0].lineContent" value=""/>
<input type="hidden" name="[1].lineContent" value=""/>
<input type="hidden" name="[2].lineContent" value=""/>
</form>
Each time my for loop for page iterates, my for loops in lines get reset to 0, this creates duplicate name entries.
There are multiple ways to correct this, the easiest:
Build input by hand with unique name id instead of using HTML helpers
My question is the following: how can I achieve a clean razor that can be reused, and that will not duplicate name entries in my form?
UPDATE:
Context:
Just like the book exemple up, my models have the same structure.
in my case I have a Submission that contains a list of Section that contains a list of SubSection that contains a list of SubmissionLine.
Here's my main view:
#model
QuotingPlus.Models.Submission
#{
ViewData["Title"] = "Edit";
}
<form id="submission-form" asp-action="Edit">
<div>
<p class="d-inline-block">
<a class="btn btn-primary" data-toggle="collapse"
href="#multiCollapseExample1" role="button" aria-expanded="false" aria-controls="multiCollapseExample1">Edit submission details</a>
</p>
<nav aria-label="breadcrumb" class="d-inline-block">
<ol class="breadcrumb">
<li class="breadcrumb-item">
<a href="/Clients/Details/#(Model.IdProjectNavigation.IdClientNavigation.IdClient)">#Model.IdProjectNavigation.IdClientNavigation.FirstName
#Model.IdProjectNavigation.IdClientNavigation.LastName</a>
</li>
<li class="breadcrumb-item">
#Model.IdProjectNavigation.Name
</li>
<li class="breadcrumb-item active" aria-current="page">#Model.Number</li>
</ol>
</nav>
</div>
<div class="row">
<div class="col mb-3">
<div class="collapse multi-collapse" id="multiCollapseExample1">
<div class="card card-body">
<h1>Submission details</h1>
<hr/>
<div class="row">
<div class="col-md-4">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="IdSubmission"/>
<div class="form-group">
<label asp-for="IdTypeSubmission" class="control-label"></label>
<select asp-for="IdTypeSubmission" class="form-control" asp-items="ViewBag.IdTypeSubmission"></select>
<span asp-validation-for="IdTypeSubmission" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="IdProject" class="control-label"></label>
<select asp-for="IdProject" class="form-control" asp-items="ViewBag.IdProject"></select>
<span asp-validation-for="IdProject" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Number" class="control-label"></label>
<input asp-for="Number" class="form-control"/>
<span asp-validation-for="Number" class="text-danger"></span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="save-warning" style="display: none;" class="alert alert-info alert-dismissible fade show" role="alert">
<strong>WARNING!</strong> Make sure you save before leaving.
<button type="button" class="close" data-dismiss="alert" aria-label="Close" onclick="SetWarningDisplayPreferenceCookie();">
<span aria-hidden="true">×</span>
</button>
</div>
<div id="carouselIndicators" style="height: 100% !important;" class="carousel slide pb-5 mb-5" data-interval="false">
<div class="carousel-inner">
#{
Html.RenderPartial("SubmissionSectionEditor", Model.SubmissionSection.ToList());
}
</div>
<nav class="navbar navbar-light bg-secondary mb-0 pt-2 fixed-bottom">
<div>
<button type="submit" value="Save" class="btn btn-default text-white">
<i class="material-icons" style="font-size: 2em;">
save
</i>
</button>
</div>
<a class="col text-center mh-100 pt-2 pb-2" href="#carouselIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="false"></span>
</a>
<a class="col text-center mh-100 pt-2" href="#carouselIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="false"></span>
</a>
<div>
<a asp-action="Index" class="btn text-white">
<i class="material-icons" style="font-size: 2em;">
cancel
</i>
</a>
</div>
</nav>
</div>
</form>
Here's SubmissionSectionEditor.cshtml
#model List<SubmissionSection>
#{
var isFirstCarouselItem = true;
}
#for (var indexSection = 0; indexSection < Model.Count(); indexSection++)
{
<div class="carousel-item #((isFirstCarouselItem) ? "active" : "")">
#{
isFirstCarouselItem = false;
}
<h1>#Model[indexSection].IdSectionNavigation.Name</h1>
<div id="#(Model[indexSection].IdSection + "accordion")">
#{
var submissionSubSections = M odel[indexSection].SubmissionSubSection;
}
#if (submissionSubSections != null)
{
Html.RenderPartial("SubmissionSubSectionEditor", submissionSubSections.ToList());
}
</div>
</div>
}
Here's SubmissionSubSectionEditor.cshtml
#model List<SubmissionSubSection>
#for (var indexSubSection = 0; indexSubSection < Model.Count; indexSubSection++)
{
<div class="card">
<div class="card-header" id="#(Model[indexSubSection].IdSubSection + "SubSectionHeader")">
<h2 class="mb-0">
<button type="button" class="btn btn-link collapsed" data-toggle="collapse" data-target="#("#" + Model[indexSubSection].IdSubSection + "SubSectionCollapse")" aria-
expanded="true" aria-controls="#(Model[indexSubSection].IdSubSection + "SubSectionCollapse")">
#Model[indexSubSection].IdSubSectionNavigation.Name
</button>
</h2>
</div>
<div id="#(Model[indexSubSection].IdSubSection + "SubSectionCollapse")" class="collapse" aria-labelledby="#(Model[indexSubSection].IdSubSection +
"SubSectionHeader")" data-parent="#("#" + Model[indexSubSection].IdSubmissionSectionNavigation.IdSection + "accordion")">
<div class="card-body">
<table class="table w-100">
<thead class="thead-dark">
<tr>
<th>Quantity</th>
<th>Article</th>
<th>Total Material</th>
<th>Unit Price Material</th>
<th>Total Sub Contractor</th>
<th>Unit Price Sub Contractor</th>
<th>Total Workforce</th>
<th>Unit Price Workforce</th>
<th>Display</th>
</tr>
</thead>
<tbody>
#{
Html.RenderPartial("SubmissionLineEditor", Model[indexSubSection].SubmissionLine.ToList());
}
</tbody>
</table>
</div>
</div>
</div>
}
Here's SubmissionLineEditor.cshtml
#model List<SubmissionLine>
#for (var indexLine = 0; indexLine < Model.Count; indexLine++)
{
<tr>
#Html.HiddenFor(x => Model[indexLine].IdSubmissionLine)
#Html.HiddenFor(x => Model[indexLine].IdArticle)
#Html.HiddenFor(x => Model[indexLine].IdSubmissionSubSection)
<td>#Html.TextBoxFor(x => Model[indexLine].Quantity, new {#type = "number", #step = "0.5", #min="0"})</td>
<td>#Model[indexLine].IdArticleNavigation.Designation</td>
<td>#Model[indexLine].TotalMaterial</td>
<td>#Model[indexLine].IdArticleNavigation.UnitPriceMaterial</td>
<td>#Model[indexLine].TotalSubContractor</td>
<td>#Model[indexLine].IdArticleNavigation.UnitPriceSubContractor</td>
<td>#Model[indexLine].TotalWorkforce</td>
<td>#Model[indexLine].IdArticleNavigation.UnitPriceWorkforce</td>
<td>#Html.CheckBoxFor(x => Model[indexLine].IsDisplayed, new {#class = "checkbox"}).
</td>
</tr>
}
I only want to know how I can keep this structure with MVVM and avoid the input name duplicate issue?
RE-UPDATE:
I'm not quite sure why this would be useful since I get name duplicate input, but here's my save action:
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin, SuperAdmin, Employe")]
public ActionResult Edit(int id, Submission submission, [FromForm] List<SubmissionLine> lines)
{
var test = Request.Form;
if (id != submission.IdSubmission)
{
return NotFound();
}
if (ModelState.IsValid)
{
SubmissionUpdateHelper.SaveSubmissionModifications(_context, submission, lines);
return RedirectToAction(nameof(Index));
}
ViewData["IdProject"] = new SelectList(_context.Project, "IdProject", "Name", submission.IdProject);
ViewData["IdTypeSubmission"] = new SelectList(_context.TypeSubmission, "IdTypeSubmission",
"TypeSubmission1", submission.IdTypeSubmission);
return View(submission);
}
For showing sub-list properties in View, Try code below:
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="BookName" class="control-label"></label>
<input asp-for="BookName" class="form-control" />
<span asp-validation-for="BookName" class="text-danger"></span>
</div>
<div class="form-group">
#for (int i = 0; i < Model.Pages.Count; i++)
{
<div class="form-group">
<label asp-for="Pages[i].PageName" class="control-label"></label>
<input asp-for="Pages[i].PageName" class="form-control" />
<span asp-validation-for="Pages[i].PageName" class="text-danger"></span>
</div>
<div class="form-group">
#for (int j = 0; j < Model.Pages[i].Lines.Count; j++)
{
<div class="form-group">
<label asp-for="Pages[i].Lines[j].LineContent" class="control-label"></label>
<input asp-for="Pages[i].Lines[j].LineContent" class="form-control" />
<span asp-validation-for="Pages[i].Lines[j].LineContent" class="text-danger"></span>
</div>
}
</div>
}
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
Finally, I've just added the input by hand so I could keep my MVVM pattern. I've also used an increment variable in TempData so I could keep track of the input
in Edit.cshtml
#{
TempData["submissionLineCount"] = 0;
}
here's the new version of SubmissionLineEditor.cshtml
#model List<SubmissionLine>
#for (var indexLine = 0; indexLine < Model.Count; indexLine++)
{
<tr>
<input name="[#(TempData["submissionLineCount"])].IdSubmissionLine" value="#(Model[indexLine].IdSubmissionLine)" type="hidden"/>
<input name="[#(TempData["submissionLineCount"])].IdArticle" value="#(Model[indexLine].IdArticle)" type="hidden"/>
<input name="[#(TempData["submissionLineCount"])].IdSubmissionSubSection" value="#(Model[indexLine].IdSubmissionSubSection)" type="hidden"/>
<td>#TempData["submissionLineCount"]</td>
<td>
<input name="[#(TempData["submissionLineCount"])].Quantity" value="#(Model[indexLine].Quantity)" type="number" step="0.5" min="0"/>
</td>
<td>#Model[indexLine].IdArticleNavigation.Designation</td>
<td>#Model[indexLine].TotalMaterial</td>
<td>#Model[indexLine].IdArticleNavigation.UnitPriceMaterial</td>
<td>#Model[indexLine].TotalSubContractor</td>
<td>#Model[indexLine].IdArticleNavigation.UnitPriceSubContractor</td>
<td>#Model[indexLine].TotalWorkforce</td>
<td>#Model[indexLine].IdArticleNavigation.UnitPriceWorkforce</td>
<td>
<input type="checkbox" name="[#(TempData["submissionLineCount"])].IsDisplayed" class="checkbox" value="#(Model[indexLine].IsDisplayed)"/>
</td>
</tr>
{
TempData["submissionLineCount"] = (Convert.ToInt32(TempData["submissionLineCount"]) + 1);
}
}
This is clearly not the best way, I hope there will be more documentation on how to apply MVVM in AspNet.Core in the future...
If this answer gets deprecated, make sure to post your own!
I know there are a lot of questions surrounding this topic online however I cannot get any of the solutions to work for me.I have tried numerous way suggested online. I have a master page with my navigation bar code and I want the active link to be highlighted in green when I am on a particular page of the website. I can't figure out what I am doing wrong or missing to get this to work. I am new to ASP.Net and C# so any help would be appreciated.
Here is my current code in body tags of my master page:
<script>
$(document).ready(function () {
var url = window.location;
$('.sidebar .nav').find('.active').removeClass('active');
$('.sidebar .nav li a').each(function () {
if (this.href == url) {
$(this).parent().addClass('active');
}
});
});
</script>
<form id ="form1" runat="server">
<div class="wrapper">
<div class="sidebar" data-color="green" data-image="../assets/img/side-navship.jpg">
<div class="logo">
<a href="Default.aspx" class="simple-text">
Speed-E
</a>
</div>
<div class="sidebar-wrapper">
<ul class="nav">
<li class="">
<a href="<%= Page.ResolveUrl("~/Default.aspx") %>">
<i class="material-icons">dashboard</i>
<p>Home</p>
</a>
</li>
<li class="">
<a href="<%= Page.ResolveUrl("~/NewCert.aspx") %>">
<i class="material-icons">content_paste</i>
<p>New Certificate</p>
</a>
</li>
</ul>
</div>
</div>
<div class="main-panel">
<nav class="navbar navbar-transparent navbar-absolute">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"> </a>
</div>
</div>
</nav>
<div class="content">
<div class="container-fluid">
<asp:ContentPlaceHolder ID="BodyContent"runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
</div>
</div>
</form>
i have this html
<div class="form-wrapper">
<div class="clearfix">
<div class="row">
<div class="time-wrapper col-xs-6">
<div class="row">
<div class="text-left col-md-6 cols-sm-12">
<input type="radio" id="flight-return-1" name="flight-return" data-default-meal="X">
<div class="">
<div class="date pad-left-large-md no-padding-left-xs white-space-nowrap">Za. 06 May. 2017</div>
</div>
</div>
<div class="flight date text-right-md text-left-xs col-md-6 cols-sm-12 pad-right-large">
<span>
bet </span>
<span class="time">
12:10 </span>
</div>
</div>
</div>
<div class="time-wrapper col-xs-6">
<div class="row">
<div class="flight date text-md-left text-sm-right no-padding-left col-md-7 cols-sm-12">
<span class="time">
14:25 </span>
<span>
zeb </span>
</div>
<div class="price-wrapper col-md-5 cols-sm-12">
<div class="price text-right white-space-nowrap">
<span class="currency symbol">€</span> <span class="integer-part">69</span><span class="decimal-part">,99</span> </div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Please note that i have multiples <div class="row">inside one .
i want to get all the data there
i'm using this c# code :
var node_1 = Doc.DocumentNode.SelectNodes("//div[#class='form-wrapper']").First();
var ITEM = node_1.SelectNodes("//div[#class='clearfix']");
foreach (var Node in node_1.SelectNodes("//div[#class='clearfix']"))
{
Console.WriteLine(Node.SelectNodes("//span[#class='time']")[1].InnerText.Trim());
}
I'm only trying to get all the times (there is like 4 class(clearfix) )
so i'm expecting dates like :
14:25
18:25
17:50
13:20
but for some reasons i only get :
14:25
14:25
14:25
14:25
it keeps repeating this i m stuck at this
thanks in advance
The double forward slash in the XPATH of your Console.WriteLine statement ("//span[....") is running out of your current node context and returning the first instance in the whole document that matches your XPATH.
Try to make your second XPATH relative (best way is to debug the code and examine what was returned into the Node variable in the loop)
You could also just iterate the spans directly:
foreach (var spanNode in node_1.SelectNodes("//span[#class='time']"))
{
Console.WriteLine(spanNode.InnerText.Trim());
}
you are passing index statically this will be the issue
Node.SelectNodes("//span[#class='time']")[1].InnerText.Trim()//Here [1] you are passing statically
This question already has answers here:
RegEx match open tags except XHTML self-contained tags
(35 answers)
Closed 9 years ago.
I am trying to scrape specific html tags including their data from a google products page. I want to get all the <li> tags within this ordered list and put them in a list.
Here is the code:
<td valign="top">
<div id="center_col">
<div id="res">
<div id="ires">
<ol>
<li class="g">
<div class="pslires">
<div class="psliimg">
<a href=
"https://www.google.com">
</a>
</div>
<div class="psliprice">
<div>
<b>$59.99</b> used
</div><cite>google auctions</cite>
</div>
<div class="pslimain">
<h3 class="r"><a href=
"https://www.google.com">
google</a></h3>
<div>
dummy data </div>
</div>
</div>
</li>
<li class="g">
<div class="pslires">
<div class="psliimg">
<a href=
"https://www.google.com">
</a>
</div>
<div class="psliprice">
<div>
<b>$59.99</b> used
</div><cite>google auctions</cite>
</div>
<div class="pslimain">
<h3 class="r"><a href=
"https://www.google.com">
google</a></h3>
<div>
dummy data </div>
</div>
</div>
</li>
<li class="g">
<div class="pslires">
<div class="psliimg">
<a href=
"https://www.google.com">
</a>
</div>
<div class="psliprice">
<div>
<b>$59.99</b> used
</div><cite>google auctions</cite>
</div>
<div class="pslimain">
<h3 class="r"><a href=
"https://www.google.com">
google</a></h3>
<div>
dummy data </div>
</div>
</div>
</li>
<li class="g">
<div class="pslires">
<div class="psliimg">
<a href=
"https://www.google.com">
</a>
</div>
<div class="psliprice">
<div>
<b>$59.99</b> used
</div><cite>google auctions</cite>
</div>
<div class="pslimain">
<h3 class="r"><a href=
"https://www.google.com">
google</a></h3>
<div>
dummy data </div>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
<div id="foot">
<p class="flc" id="bfl" style="margin:19px 0 0;text-align:center"><a href=
"/support/websearch/bin/answer.py?answer=134479&hl=en">Search Help</a>
<a href=
"/quality_form?q=Pioneer+Automotive+PF-555-2000&hl=en&tbm=shop">Give us
feedback</a></p>
<div class="flc" id="fll" style="margin:19px auto 19px auto;text-align:center">
Google Home <a href=
"/intl/en/ads">Advertising Programs</a> <a href="/services">Business
Solutions</a> Privacy & Terms <a href=
"/intl/en/about.html">About Google</a>
</div>
</div>
</td>
I want to get all the <li class="g"> tags and the data in each of them. Is that possible?
instead of using a regex using something like an xml parser may be more useful to your situation. Load it up into an xml document and then use something like SelectNodes to get out your data you are looking for
http://msdn.microsoft.com/en-us/library/4bektfx9.aspx
I wouldn't use regex for this particular problem.
Instead I would attack it thus:
1)Save off page as html string.
2)Use aforementioned htmlagilitypack or htmltidy(my preference) to convert to XML.
3)Use xDocument to navigate through Dom object by tag and save data.
Trying to create a regex to extract data from a possibly fluid HTML page will break your heart.
Instead of using regex you can use HtmlAgilityPack to parse the HTML.
var doc = new HtmlDocument();
doc.LoadHtml(html);
var listItems = doc.DocumentNode.SelectNodes("//li");
The code above will give you all <li> items in the document. To add them to a list you'll just have to iterate the collection and add each item to the list.