Html Agility Pack c# - c#

I want to extract the number of items in stock (td5) from every product,
and having a problem that the system isnt writing 0 in the table if stock is empty.
My Code to extract the number would be:
HtmlNode node = doc.DocumentNode.SelectSingleNode("//tr[1]//td[5]");
With this I get product2 items in stock of course, any1 got a trick for me
to preprocess the table and fill the td´s with 0 or another way to
grab the right numbers to the right product?
HTML:
<tr>
<td class="data">product1</td>
<td class="data" width="350">TEST1</td>
<td class="data">27</td>
<td class="data">ST</td>
<td class="data"></td>
<!-- <td class=data>`LIST-KWERT[j].value`</td> -->
<td class="data"></td>
<!-- <td class=data>`LIST-SEINM[j].value`</td> -->
<!-- <td class=data>`LIST-SSPEM[j].value`</td> -->
<td class="data"></td>
<td class="data"></td>
<td class="data"></td>
<td class="data">999,9</td>
</tr>
<tr>
<td class="data">product2</td>
<td class="data" width="350">TEST2</td>
<td class="data">13</td>
<td class="data">ST</td>
<td class="data">21,000</td>
<!-- <td class=data>`LIST-KWERT[j].value`</td> -->
<td class="data"></td>
<!-- <td class=data>`LIST-SEINM[j].value`</td> -->
<!-- <td class=data>`LIST-SSPEM[j].value`</td> -->
<td class="data">5,000</td>
<td class="data">10,000</td>
<td class="data">243</td>
<td class="data">35,5</td>
</tr>

If you want to get 5th td in each row you use this code:
var html = new HtmlDocument();
html.LoadHtml(input);
var root = html.DocumentNode;
var list = new List<Data>();
foreach (var node in root.SelectNodes("//tr//td[5]"))
{
var price = node.InnerText.IsNullOrWhiteSpace() ? "no price" : node.InnerText;
}

Related

SMTP mail not displaying the image

I am new to web api ,Here I am sending mail using SMTP service .
In this I need to send images I mean I need to display the image along with the mail contents.
But in my case contents are properly displaying but the image is not .
The data of image URL is : http://192.168.168.62:8087/Images/Product/My_Cart/axe_brand_1.jpg
When I inspect the image in the mail box it shows as follows:
<img data-imagetype="External" src="/actions/ei?u=http%3A%2F%2F192.168.168.62%3A8087%2FImages%2FProduct%2FMy_Cart%2Faxe_brand_1.jpg&d=2019-01-02T05%3A52%3A02.212Z" originalsrc="http://192.168.168.62:8087/Images/Product/My_Cart/axe_brand_1.jpg" data-connectorsauthtoken="1" data-imageproxyendpoint="/actions/ei" data-imageproxyid="">
I have tried many ways to solve this I can't .Can anyone help me to solve this .
Update :
c#:
foreach (DataRow Row in dt.Tables[0].Rows)
{
LinkedResource res = new LinkedResource(Row["ITEM_IMAGE"].ToString());
res.ContentId = Guid.NewGuid().ToString();
string htmlBody = #"<img src='cid:" + res.ContentId + #"'/>";
tableRows.AppendFormat(mailBodyTemplate, htmlBody, Row["ITEM_NAME"], Row["ITEM_UOM"], Row["QUANTITY"], Row["ITEM_PRICE_WITH_GST"], Row["TOTAL_AMOUNT_WITH_GST"]);
}
var mailBody = string.Format(PurchaseSummary, tableRows.ToString(), totalPrice);
Template :
<tr>
<td align="center"><img src="{0}" /></td>
<td>
<table style="width:100%;margin-left:15px">
<tr>
<td align="right" style="width:50%">Product Name :</td>
<td align="left" style="color:#32CD32;font-weight:bold;width:50%">{1}</td>
</tr>
<tr>
<td align="right" style="width:50%">Product UOM :</td>
<td align="left" style="color:#696969;font-weight:bold;width:50%">{2}</td>
</tr>
<tr>
<td align="right" style="width:50%">Quantity :</td>
<td align="left" style="color:#778899;font-weight:bold;width:50%">{3}</td>
</tr>
<tr>
<td align="right" style="width:50%">Unit Price :</td>
<td align="left" style="color:#483D8B;font-weight:bold;width:50%">{4}
<td>
</tr>
</table>
</td>
<td align="center" style="font-size:20px;font-weight:bold;color:#1E90FF">{5}</td>
</tr>
still it's not displaying the image .
While inspecting the image I got as follows..
<img data-imagetype="External" src="/actions/ei?u=http%3A%2F%2F192.168.168.62%3A8087%2FImages%2FProduct%2FMy_Cart%2Faxe_brand_1.jpg&d=2019-01-02T09%3A41%3A12.195Z" originalsrc="http://192.168.168.62:8087/Images/Product/My_Cart/axe_brand_1.jpg" data-connectorsauthtoken="1" data-imageproxyendpoint="/actions/ei" data-imageproxyid="">

Select Node with size of Its specific child Node In Linq, HtmlAgilityPack

I'm trying to get following data.
<html>
<body>
<tr class="udline">
<th rowspan="2" class="noln">시간</th>
<th rowspan="2">개인</th>
<th rowspan="2">외국인</th>
<th rowspan="2">기관계</th>
<th colspan="6" class="eb">기관</th>
<th rowspan="2">기타법인</th>
</tr>
<tr class="udline">
<th class="sub">금융투자</th>
<th class="sub">보험</th>
<th class="sub">투신<br>(사모)</th>
<th class="sub">은행</th>
<th class="sub">기타금융기관</th>
<th class="sub">연기금등</th>
</tr>
<tr>
<td colspan="11" class="blank_07"></td>
</tr>
<!-- following are data -->
<tr>
<td class="date2">18:01</td>
<td class="rate_up3">2,024</td>
<td class="rate_down3">-3,307</td>
<td class="rate_up3">1,116</td>
<td class="rate_up3">824</td>
<td class="rate_down3">-16</td>
<td class="rate_up3">764</td>
<td class="rate_down3">-43</td>
<td class="rate_down3">-5</td>
<td class="rate_down3">-408</td>
<td class="rate_up3">166</td>
</tr>
<tr>
<td class="date2">18:00</td>
<td class="rate_up3">2,022</td>
<td class="rate_down3">-3,305</td>
<td class="rate_up3">1,116</td>
<td class="rate_up3">824</td>
<td class="rate_down3">-16</td>
<td class="rate_up3">764</td>
<td class="rate_down3">-43</td>
<td class="rate_down3">-5</td>
<td class="rate_down3">-408</td>
<td class="rate_up3">166</td>
</tr>
...
</body></html>
I want to get Nodes list of "tr" tag which has a data. but I have problem with getting "tr" tag.
I think it is enough if I can get sets of "tr" which has 11 td tags.
so I write following source.
result = await httpClient.GetStringAsync(new Uri(timeUrlAddress));
htmlDoc.LoadHtml(result);
var nodes =
htmlDoc.DocumentNode.SelectNodes("//tr")
.Where(i => i.ChildNodes.Any(j => j.Name.Equals("td")).Count>10); // <--- I have Problem.
foreach(var i in nodes) { ... } // <-- iterating list of <tr> tags.
and It doesn't work.
I could get List of tr tag with DoucmentNode.SelectNodes("//tr") ... and I appended .Where(i=>i.ChildNodes.Count >10 ) to get what i want.
but tr has several "text"childNodes and I get Unwanted Node. following picture shows that I got with .Where(i=>i.ChildNodes.Count>10).
I want to get tr node that has td tag as child nodes and has exactly 11 of td tag.
how can I get that tr nodes with Linq syntax..?
If you want tr node with exactrly 11 td children you can use below XPath:
//tr[count(td) = 11]

Add tbody XML Element to table Element in XDcoument

Want to add <tbody> element in <table> elements if missing on Xdcoument.
<table class="newtable" id="item_559_Table1" cellpadding="0" cellspacing="0" data-its-style="width:11.4624em; border-spacing:0;">
<colgroup data-its-style="width:11.4624em; " />
<tr>
<td data-its-style="padding:0.2292em; vertical-align:top; ">
<p data-its-style="">My dad cooks up a pot of chicken soup, and</p>
</td>
</tr>
<tr>
<td data-its-style="padding:0.2292em; vertical-align:top; ">
<p data-its-style="font-weight:normal; ">This cold means I can’t taste a thing today!</p>
</td>
</tr>
</table>
Output should look like
<table class="newtable" id="item_559_Table1" cellpadding="0" cellspacing="0" data-its-style="width:11.4624em; border-spacing:0;">
<colgroup data-its-style="width:11.4624em; " />
<tbody>
<tr>
<td data-its-style="padding:0.2292em; vertical-align:top; ">
<p data-its-style="">My dad cooks up a pot of chicken soup, and</p>
</td>
</tr>
<tr>
<td data-its-style="padding:0.2292em; vertical-align:top; ">
<p data-its-style="font-weight:normal; ">This cold means I can’t taste a thing today!</p>
</td>
</tr>
</tbody>
</table>
**Not looking for XSLT solution.
One way to do it would be to grab the children of <table>, then add them back they way you want them.
var doc = XDocument.Load("file.xml");
var colgroup = doc.Root.Elements("colgroup");
var tr = doc.Root.Elements("tr");
// Add tr to tbody
var tbody = new XElement("tbody", tr);
// Replace the children of table with colgroup and tbody
doc.Root.ReplaceNodes(colgroup, tbody);

How to read <table> into 'onmouseover' event with C# and HTMLAgilityPack

How to read <table> into onmouseover event with C# and HTMLAgilityPack?
markup code :
<a href="#" class="chan_live_not_free" onclick="return false;" onmouseover="return overlib('
<table>
<tr class=fieldRow>
<td class=posH_col width=40>
<strong>pos</strong>
</td>
<td class=rest_col width=90>
<strong>satellite</strong>
</td>
<td class=freqH_col width=50>
<strong>freq</strong>
</td>
<td class=rest_col width=90>
<strong>symbol</strong>
</td>
<td class=rest_col width=90>
<strong>encryption</strong>
</td>
</tr>
<tr>
<td class="pos_col">39.0°e</td>
<td class=rest_col>Hellas Sat 2</td>
<td class="freq_col">12.606 H</td>
<td class=rest_col>30000 - 2/3</td>
<td class=enc_not_live>MPEG-4 BulCrypt</td>
</tr>
</table>',CAPTION, 'Arena Sport 4 (serbia) – 19/10/14 - 11:30');" onmouseout="return nd();">
Arena Sport 4 (serbia)
</a>
I need to read the table into onmouseover event. How does it read?
You could get the element attribute of the <a> tag with HTML Agility Pack and then using regular expressions get the <table> inside the string, something like the following code :
var html = #"<a href='#' class='chan_live_not_free' onclick='return false;' onmouseover='return overlib(
<table>
<tr class=fieldRow>
<td class=posH_col width=40>
<strong>pos</strong>
</td>
<td class=rest_col width=90>
<strong>satellite</strong>
.
.
.
<tr>
<td class="pos_col">39.0°e</td>
<td class=rest_col>Hellas Sat 2</td>
<td class="freq_col">12.606 H</td>
<td class=rest_col>30000 - 2/3</td>
<td class=enc_not_live>MPEG-4 BulCrypt</td>
</tr>
</table>,CAPTION, 'Arena Sport 4 (serbia) – 19/10/14 - 11:30');' onmouseout='return nd();'>
Arena Sport 4 (serbia)
</a>";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var value = doc.DocumentNode.SelectSingleNode("//a[#class='chan_live_not_free']").Attributes["onmouseover"].Value;
var text = Regex.Matches(value, #"<table>([^)]*)</table>")[0].Value;

Modify Existing HTML Table

I have an HTML table that I request from a webserver in C#. I am then displaying the page in my aspx webform. How can I add a prerequisite based on the course ID to the last column in the table without hard-coding the prerequisite? Example of the table design is below.
<tr bgcolor="#E1E1CC">
<td width="7%">003597</td>
<td width="5%">01</td>
<td width="1%">OPT</td>
<td width="8%">MT H </td>
<td width="16%">2:00 pm - 2:50 pm </td>
<td width="17%">08/26/13 - 12/12/13</td>
<td width="8%">
<a href="http://www.mnsu.edu/registrar/building.html"target = _blank>
<b>TR C124</b>
</a>
</td>
<td width="19%">Staff</td>
<td width="4%">22</td>
<td width="4%">6</td>
<td width="4%"><font color="#000000">Open</font></td>
<td width="7%">
<a href=Notes.asp?SpclNote=20143+003597+IT+100 target = _blank>
<b>Notes</b>
</a>
</td>
</tr>
<tr bgcolor="#E1E1CC">
<td colspan="3"> </td>
<td width="8%">M </td>
<td width="16%">10:00 am - 11:50 am</td>
<td width="17%">08/26/13 - 12/09/13</td>
<td width="8%">
<a href="http://www.mnsu.edu/registrar/building.html"target = _blank>
<b>WH 0119 </b>
</a>
</td>
<td width="19%">Staff</td>
<td colspan="4"> </td>
</tr>
If you are getting the page html as a string you could just insert your html into it. Something like:
private void SetValue(string PageHtml, string ID, string TextToInsert)
{
string html = PageHtml;
string sMyHtmlToInsert = TextToInsert;
int iSplitIndex = html.IndexOf(ID);
iSplitIndex = html.IndexOf("{tag}",iSplitIndex);
string sHtml1 = html.SubString(0, iSplitIndex);
string sHtml2 = html.SubString(iSplitIndex);
string sFinalHtml = sHtml1 + sMyHtmlToInsert + sHtml2;
}

Categories