Removing Columns from a HTML Table - c#

I'm trying to delete the 3rd and 4th <td> and <th> from my table using HtmlAgilityPack.
Example table string:
<table>
<thead>
<tr>
<th>Item</th>
<th>Price</th>
<th>Change</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<h2>Top Menu Items</h2>
</td>
</tr>
<tr>
<td> Diced Angus Steak <span>(7oz)</span></td>
<td>$13.50</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Kimchi Cheese Beef Pepper Rice</td>
<td>$15.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Classic Beef Pepper Rice</td>
<td>$13.50</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td>
<h2>Steaks</h2>
</td>
</tr>
<tr>
<td> Angus Rib Eye Steak <span>(8oz)</span></td>
<td>$25.50</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Angus Sirloin Steak <span>(8oz)</span></td>
<td>$22.50</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Diced Angus Steak <span>(7oz)</span> <span>(Steaks)</span></td>
<td>$13.50</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Chicken Breast Steak <span>(8oz)</span></td>
<td>$14.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Premium Hamburger Steak <span>(10oz)</span></td>
<td>$16.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td>
<h2>Pepper Rice</h2>
</td>
</tr>
<tr>
<td> Sambar Pepper Rice</td>
<td>$13.50</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Kimchi Cheese Beef Pepper Rice <span>(Pepper Rice)</span></td>
<td>$15.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Chicken Pepper Rice</td>
<td>$13.50</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Salmon Pepper Rice</td>
<td>$15.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Classic Beef Pepper Rice <span>(Pepper Rice)</span></td>
<td>$13.50</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td>
<h2>Sides</h2>
</td>
</tr>
<tr>
<td> Rice</td>
<td>$3.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Miso Soup</td>
<td>$3.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Sauteed String Beans</td>
<td>$4.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Sauteed Corn</td>
<td>$4.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Kimchi</td>
<td>$5.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> French Fries</td>
<td>$4.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Onion Rings</td>
<td>$5.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Deep Fried Dumpling</td>
<td>$8.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Sausages</td>
<td>$7.50</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td>
<h2>Salad</h2>
</td>
</tr>
<tr>
<td> Large Salad</td>
<td>$7.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Small Salad</td>
<td>$3.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Large Seaweed Salad</td>
<td>$9.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
<tr>
<td> Small Seaweed Salad</td>
<td>$5.00</td>
<td>
- -
</td>
<td>
<span>
</span>
<span>
</span>
</td>
</tr>
</tbody>
</table>
I send the following string to this method, to remove the 3rd and 4th <td> and <th>.
public static string deleteCols(string table)
{
var doc = new HtmlDocument();
doc.LoadHtml(table);
bool first = true;
foreach (HtmlNode row in doc.DocumentNode.SelectNodes("//tr"))
{
if (first)
{
try
{
var th3 = row.SelectSingleNode("th[3]");
row.RemoveChild(th3);
}
catch
{
}
try
{
var th4 = row.SelectSingleNode("th[4]");
row.RemoveChild(th4);
}
catch
{
}
first = false;
}
else
{
try
{
var td3 = row.SelectSingleNode("td[3]");
row.RemoveChild(td3);
}
catch
{
}
try
{
var td4 = row.SelectSingleNode("th[4]");
row.RemoveChild(td4);
}
catch
{
}
}
}
foreach (HtmlNode row2 in doc.DocumentNode.SelectNodes("//span"))
{
row2.Remove();
}
return doc.DocumentNode.InnerHtml;
}
Which gives me the following result:
<table>
<thead>
<tr>
<th>Item</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<h2>Top Menu Items</h2>
</td>
</tr>
<tr>
<td> Diced Angus Steak </td>
<td>$13.50</td>
<td>
</td>
</tr>
<tr>
<td> Kimchi Cheese Beef Pepper Rice</td>
<td>$15.00</td>
<td>
</td>
</tr>
<tr>
<td> Classic Beef Pepper Rice</td>
<td>$13.50</td>
<td>
</td>
</tr>
<tr>
<td>
<h2>Steaks</h2>
</td>
</tr>
<tr>
<td> Angus Rib Eye Steak </td>
<td>$25.50</td>
<td>
</td>
</tr>
<tr>
<td> Angus Sirloin Steak </td>
<td>$22.50</td>
<td>
</td>
</tr>
<tr>
<td> Diced Angus Steak </td>
<td>$13.50</td>
<td>
</td>
</tr>
<tr>
<td> Chicken Breast Steak </td>
<td>$14.00</td>
<td>
</td>
</tr>
<tr>
<td> Premium Hamburger Steak </td>
<td>$16.00</td>
<td>
</td>
</tr>
<tr>
<td>
<h2>Pepper Rice</h2>
</td>
</tr>
<tr>
<td> Sambar Pepper Rice</td>
<td>$13.50</td>
<td>
</td>
</tr>
<tr>
<td> Kimchi Cheese Beef Pepper Rice </td>
<td>$15.00</td>
<td>
</td>
</tr>
<tr>
<td> Chicken Pepper Rice</td>
<td>$13.50</td>
<td>
</td>
</tr>
<tr>
<td> Salmon Pepper Rice</td>
<td>$15.00</td>
<td>
</td>
</tr>
<tr>
<td> Classic Beef Pepper Rice </td>
<td>$13.50</td>
<td>
</td>
</tr>
<tr>
<td>
<h2>Sides</h2>
</td>
</tr>
<tr>
<td> Rice</td>
<td>$3.00</td>
<td>
</td>
</tr>
<tr>
<td> Miso Soup</td>
<td>$3.00</td>
<td>
</td>
</tr>
<tr>
<td> Sauteed String Beans</td>
<td>$4.00</td>
<td>
</td>
</tr>
<tr>
<td> Sauteed Corn</td>
<td>$4.00</td>
<td>
</td>
</tr>
<tr>
<td> Kimchi</td>
<td>$5.00</td>
<td>
</td>
</tr>
<tr>
<td> French Fries</td>
<td>$4.00</td>
<td>
</td>
</tr>
<tr>
<td> Onion Rings</td>
<td>$5.00</td>
<td>
</td>
</tr>
<tr>
<td> Deep Fried Dumpling</td>
<td>$8.00</td>
<td>
</td>
</tr>
<tr>
<td> Sausages</td>
<td>$7.50</td>
<td>
</td>
</tr>
<tr>
<td>
<h2>Salad</h2>
</td>
</tr>
<tr>
<td> Large Salad</td>
<td>$7.00</td>
<td>
</td>
</tr>
<tr>
<td> Small Salad</td>
<td>$3.00</td>
<td>
</td>
</tr>
<tr>
<td> Large Seaweed Salad</td>
<td>$9.00</td>
<td>
</td>
</tr>
<tr>
<td> Small Seaweed Salad</td>
<td>$5.00</td>
<td>
</td>
</tr>
</tbody>
</table>
As you can see, some of the elements I wish to delete are still there. Does anybody know what I'm doing wrong here?!

When you remove the 3rd th/tds from the row's children, the 4th item becomes the 3rd, so you're trying to remove a non-existing element.
As a solution, you can either store the elements in variables at first, and then delete them; or you can start removing from the 4th index.

Related

Bootstrap carousel control's shape shows different between server and localhost

I'm using Bootstrap3 to working on my home view page in Visual Studio, but the carousel control icon shows different between sever side(looks like square) and localhost(looks normal), is anything I missed? Thank you so much for the help!
I attached the carousel part of my code below:
<div class="col-lg-5">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
<li data-target="#carousel-example-generic" data-slide-to="3"></li>
</ol> -->
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<table id="Report_InvHardCatExpiring-table" class="table table-striped styleTable table-hover" style="border:2px;">
<caption>Hardware expiring 90 days out.</caption>
<thead>
<tr bgcolor="#a0bac4">
<th>
#Html.LabelFor(model => model.Report_InvHardwareAggregateCat.FirstOrDefault().InvHardwareCatName)
</th>
<th>
#Html.LabelFor(model => model.Report_InvSoftwareAggregateCat.FirstOrDefault().CountType)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Report_InvHardwareAggregateCat)
{
<tr>
<td>
#item.InvHardwareCatName
</td>
<td>
#item.CountType
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="item">
<table id="Report_InvSoftCatExpiring-table" class="table table-striped styleTable table-hover" style="border:2px;">
<caption>Software expiring 90 days out.</caption>
<thead>
<tr bgcolor="#a0bac4">
<th>
#Html.LabelFor(model => model.Report_InvSoftwareAggregateCat.FirstOrDefault().InvSoftwareCatName)
</th>
<th>
#Html.LabelFor(model => model.Report_InvSoftwareAggregateCat.FirstOrDefault().CountType)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Report_InvSoftwareAggregateCat)
{
<tr>
<td>
#item.InvSoftwareCatName
</td>
<td>
#item.CountType
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="item">
<table id="Report_InvFurnitureCatExpiring-table" class="table table-striped styleTable table-hover" style="border:2px;">
<caption>Furniture expiring 90 days out.</caption>
<thead>
<tr bgcolor="#a0bac4">
<th>
#Html.LabelFor(model => model.Report_InvFurnitureAggregateCat.FirstOrDefault().InvFurnitureCatName)
</th>
<th>
#Html.LabelFor(model => model.Report_InvFurnitureAggregateCat.FirstOrDefault().CountType)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Report_InvFurnitureAggregateCat)
{
<tr>
<td>
#item.InvFurnitureCatName
</td>
<td>
#item.CountType
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="item">
<table id="Report_InvHardware_ByProdTypeByAmount-table" class="table table-striped styleTable table-hover" style="border:2px;">
<caption>Hardware category by cost.</caption>
<thead>
<tr bgcolor="#a0bac4">
<th>
#Html.LabelFor(model => model.Report_InvHardware_ByProdTypeByAmount.FirstOrDefault().Name)
</th>
<th>
#Html.LabelFor(model => model.Report_InvHardware_ByProdTypeByAmount.FirstOrDefault().CountInvProductTypeID)
</th>
<th>
#Html.LabelFor(model => model.Report_InvHardware_ByProdTypeByAmount.FirstOrDefault().SumAmount)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Report_InvHardware_ByProdTypeByAmount)
{
<tr>
<td>
#item.Name
</td>
<td>
#item.CountInvProductTypeID
</td>
<td>
#item.SumAmountFormatted
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev" style="background-image: none;">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next" style="background-image: none;">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
What it shows on my server:
enter image description here
What it shows on my localhost:
enter image description here

C# IText7 HTML inside a Table Cell

i try to put HTML in a cell, but the border seems double lines:
cell = new Cell();
var elementsList = HtmlConverter.ConvertToElements(sectioncontent);
foreach (IElement e in elementsList)
{
cell.Add((IBlockElement)e);
}
cell.SetBorder(Border.NO_BORDER);
table.SetTextAlignment(TextAlignment.JUSTIFIED).AddCell(cell);
with html like this (caught from CKEditor and i put in a database):
<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
<tbody>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
<p> </p>
================================ end
String HTML = "<p>Overview line1</p>"
+ "<p>Overview line2</p><p>Overview line3</p>"
+ "<p>Overview line4</p><p>Overview line4</p>"
+ "<p>Overview line5 </p>";
String CSS = "p { font-family: Cardo; }";
cell = new Cell();
//cell.Add(new Paragraph(s));
ElementList elementsList = XMLWorkerHelper.ParseToElementList(HTML, CSS);
foreach (IElement e in elementsList)
{
cell.Add((IBlockElement)e);
}
cell.SetBorder(Border.NO_BORDER);
table.SetTextAlignment(TextAlignment.JUSTIFIED).AddCell(cell);
document.Add(table);
but it raise exception:
Unable to cast object of type 'iTextSharp.text.Paragraph' to type 'iText.Layout.Element.IElement'.
How to workaround?
regards and thanks

How to print a div in ASP.NET C#

Net c# website i have an html report is there.Now i want to take a printout of the same.So i used Javascript and its showing only the popup box ,not the content.how to solve this issue.
Javascript
<script type="text/javascript">
function PrintDiv() {
var divToPrint = document.getElementById('widget-content');
var popupWin = window.open('', '_blank', 'width=300,height=400,location=no,left=200px');
popupWin.document.open();
popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
popupWin.document.close();
}
</script>
button Click
<input type="button" onclick="PrintDiv()" value="Print" />
HTML Content
<div class="widget-content">
<div class="invoice-content">
<div class="invoice-head">
<div class="invoice-meta">
<%--Invoice <span class="invoice-number">#96558 </span><span class="invoice-date">Date:
2012-07-15</span>--%>
</div>
<h5 style="margin-left: 40%; height: 20px; font-size: large">
Order Form</h5>
<div class="invoice-to">
<ul>
<li><span>Booking Date:<asp:Label ID="dispbookingDate" runat="server"></asp:Label></span>
<span>Name<asp:Label TextMode="MultiLine" runat="server" ID="dispName"></asp:Label></span>
<span>Address:<asp:Label TextMode="MultiLine" runat="server" ID="dispAddress"></asp:Label></span>
</li>
</ul>
</div>
<div class="invoice-from">
<ul>
<li><span>Order No.<asp:Label ID="dispOrderNo" runat="server"></asp:Label></span> <span>
Wedding Date:<asp:Label runat="server" ID="dispWeddingDate"></asp:Label></span>
<span>Malayalam Date:<asp:Label runat="server" ID="dispWeddingMalayam"></asp:Label></span>
</li>
</ul>
</div>
</div>
<div>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th class="style1">
Description
</th>
<th class="style2">
Rs.
</th>
<th>
Ps.
</th>
</tr>
</thead>
<tfoot>
<tr>
<th class="total-label" colspan="2">
Total:
</th>
<th class="total-amount">
<asp:Label ID="dispTotal" runat="server"></asp:Label>
</th>
</tr>
<tr>
<th class="total-label" colspan="2">
Adavance:
</th>
<th class="total-amount">
<asp:Label ID="dispAvance" runat="server"></asp:Label>
</th>
</tr>
<tr>
<th class="total-label" colspan="2">
Balance:
</th>
<th class="total-amount">
<asp:Label ID="dispBalance" runat="server"></asp:Label>
</th>
</tr>
</tfoot>
<tbody>
<tr>
<td class="style1">
Auditorium Rent
</td>
<td class="style2">
<asp:Label ID="dispRent" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style1">
Dining Hall Rent
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Kathir Mandapam
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Tables and chairs
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Electricity charge for water
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Luxuary Tax
</td>
<td class="style2">
<asp:Label ID="dispLTax" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Central Service Tax
</td>
<td class="style2">
<asp:Label ID="dispCTax" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
<%-- <p class="amount-word">
Amount in Word: <span>
<asp:Label ID="dispAmountWord" runat="server"></asp:Label></span>
</p>--%>
</div>
<input type="button" onclick="PrintDiv()" value="Print" />
</div>
In your javascript you are searching for the div with id widget-content
your code: var divToPrint = document.getElementById('widget-content');
but in your html you have <div class="widget-content"> and it has no id, it only has a class.
So you have 2 options.
OPTION 1
Change class to id
OPTION 2
Change your javascript to search for the class like so
var divToPrint = document.getElementsByClassName('widget-content')
NOTE: this will return an array of elements with that class, whether theres only one or more.
So in order to select the one you want; assuming there is only 1 div with this class you do like so:
var divToPrint = document.getElementsByClassName('widget-content')[0]

Delete Table Column with HTML Agility Pack

I have scraped a table from a website using C# for my own website and loaded it into a string. There are too many columns so I was wondering if there was an easy way to delete some, probably using HTML Agility Pack but in C# if necessary.
The table in the string looks like this:
<table>
<tr>
<th scope="col"> </th>
<th scope="col"> </th>
<th scope="col">P </th>
<th scope="col">W </th>
<th scope="col">L </th>
<th scope="col">T </th>
<th scope="col">NR </th>
<th scope="col">Bat </th>
<th scope="col">Bowl </th>
<th scope="col">Pen </th>
<th scope="col">Pts </th>
</tr>
<tr>
<td>1 </td>
<td>Rayleigh 2nd </td>
<td>12 </td>
<td>8 </td>
<td>1 </td>
<td>0 </td>
<td>3 </td>
<td>14 </td>
<td>52 </td>
<td>0 </td>
<td>209 </td>
</tr>
<tr>
<td>2 </td>
<td>Rainham 1st </td>
<td>12 </td>
<td>8 </td>
<td>1 </td>
<td>1 </td>
<td>2 </td>
<td>12 </td>
<td>56 </td>
<td>-15 </td>
<td>199 </td>
</tr>
<tr class="lineAbove">
<td>3 </td>
<td>Old Chelmsfordians 2nd </td>
<td>12 </td>
<td>5 </td>
<td>5 </td>
<td>0 </td>
<td>2 </td>
<td>10 </td>
<td>48 </td>
<td>0 </td>
<td>148 </td>
</tr>
<tr>
<td>4 </td>
<td>Little Baddow 2nd </td>
<td>12 </td>
<td>5 </td>
<td>4 </td>
<td>0 </td>
<td>3 </td>
<td>21 </td>
<td>43 </td>
<td>-15 </td>
<td>144 </td>
</tr>
<tr>
<td>5 </td>
<td>Rayne 1st </td>
<td>12 </td>
<td>5 </td>
<td>4 </td>
<td>0 </td>
<td>3 </td>
<td>6 </td>
<td>39 </td>
<td>0 </td>
<td>140 </td>
</tr>
<tr>
<td>6 </td>
<td>Terling 1st </td>
<td>12 </td>
<td>4 </td>
<td>5 </td>
<td>1 </td>
<td>2 </td>
<td>12 </td>
<td>35 </td>
<td>0 </td>
<td>129 </td>
</tr>
<tr>
<td>7 </td>
<td>Willow Herbs 1st </td>
<td>12 </td>
<td>4 </td>
<td>6 </td>
<td>0 </td>
<td>2 </td>
<td>9 </td>
<td>34 </td>
<td>0 </td>
<td>117 </td>
</tr>
<tr>
<td>8 </td>
<td>Ongar 1st </td>
<td>12 </td>
<td>3 </td>
<td>5 </td>
<td>0 </td>
<td>4 </td>
<td>3 </td>
<td>42 </td>
<td>-5 </td>
<td>108 </td>
</tr>
<tr class="lineAbove">
<td>9 </td>
<td>Sandon Sports 1st </td>
<td>12 </td>
<td>3 </td>
<td>6 </td>
<td>0 </td>
<td>3 </td>
<td>8 </td>
<td>27 </td>
<td>0 </td>
<td>98 </td>
</tr>
<tr>
<td>10 </td>
<td>Little Waltham 2nd </td>
<td>12 </td>
<td>1 </td>
<td>9 </td>
<td>0 </td>
<td>2 </td>
<td>14 </td>
<td>25 </td>
<td>0 </td>
<td>65 </td>
</tr>
</table>
And I want to delete columns 8-10 (Bat, Bowl and Pen). I'm not really sure where to start so any pointers would be helpful!
You would need to iterate over each tr and remove the 8th, 9th and 10th td nodes from each.
bool first = true;
foreach (HtmlNode row in doc.DocumentNode.SelectNodes("//tr"))
{
if (first)
{
row.RemoveChild(row.SelectSingleNode("th[10]"));
row.RemoveChild(row.SelectSingleNode("th[9]"));
row.RemoveChild(row.SelectSingleNode("th[8]"));
first = false;
}
else
{
row.RemoveChild(row.SelectSingleNode("td[10]"));
row.RemoveChild(row.SelectSingleNode("td[9]"));
row.RemoveChild(row.SelectSingleNode("td[8]"));
}
}

Read in HTML file and replace with variables

I have an HTML file that will act as a template for an email that I am going to send out. There are fields in the html that are variable. I was wondering if there is a robust way to replace the placeholders in the HTML file with the variables. I know I could string.Replace all of them, but that isn't ideal since I have a lot of variables. Here is what the html file looks like
<html>
<head>
<title></title>
</head>
<body>
<div>
Please read the Cruise Control Details Below<br>
<br>
<table width='100%'>
<tr>
<td width='100%' colspan='5'>
<font size='4'><b>Release Details</b></font>
</td>
</tr>
<tr>
<td width='20%'>
<b>RFC Ticket #</b>
</td>
<td>
%release.RFCTicket%
</td>
<td>
</td>
<td>
</td>
<td width='10%'>
</td>
<td width='20%'>
<b>Project / Release Name</b>
</td>
<td width='20%'>
%release.ReleaseName%
</td>
</tr>
<tr>
<td width='20%'>
<b>Release Date</b>
</td>
<td width='20%'>
%release.ReleaseDateString%
</td>
<td>
</td>
<td>
</td>
<td width='10%'>
</td>
<td width='20%'>
<b>Release Time</b>
</td>
<td width='20%'>
%release.ReleaseTimeString%
</td>
</tr>
<tr>
<td width='20%'>
<b>CAB Approval Status</b>
</td>
<td width='20%'>
%release.CABApproval%
</td>
</tr>
<tr>
<td width='100%' colspan='5'>
</td>
</tr>
<tr>
<td width='100%' colspan='5'>
<font size='4'><b>Contact Information:</b></font>
</td>
</tr>
<tr>
<td width='20%'>
<b>Project / Team Lead</b>
</td>
<td width='20%'>
%release.TeamLead%
</td>
<td width='10%'>
</td>
<td width='20%'>
<b>On Call DSE</b>
</td>
<td width='20%'>
%release.OnCallDSE%
</td>
</tr>
<tr>
<td width='20%'>
<b>Phone</b>
</td>
<td width='20%'>
%release.ContactInfo%
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td width='10%'>
</td>
<td width='20%'>
<b>Phone</b>
</td>
<td width='20%'>
%release.OnCallDSEContact%
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td width='100%' colspan='5'>
<font size='4'><b>Migration Details:</b></font>
</td>
</tr>
<tr>
<td width='20%'>
<b>Deploy Dashboard</b>
</td>
<td width='20%'>
</td>
<td width='10%'>
</td>
<td width='20%'>
<td>
</td>
<td>
</td>
<b>Deploy Task</b>
</td>
<td width='20%'>
</td>
</tr>
%createTaskTable(ParseSpecialInstuctions().Split('|'))%</table>
</div>
I would like to replace the values in between the "%%" with the variable in code that represents them. I could easily
string.Replace("%release.RFCTicket%",release.RFCTicket);
But that's a bit convoluted in my opinion since I have like 10 or so variables in the file. Are there any built in methods that do what I am asking? Any help would be appreciated, thanks!
Use a regular expression to find your matches. I believe the appropriate regular expression would be along the lines of:
%release.\S+%
From there, you can examine each match, and parse the member name from the match. From there you can get the value of the member from your instance (release in this case) via reflection, and do a string replace.
Something like this. It could use some refactoring to eliminate redundant calls, and I don't know if it fully works, but you get the idea...
var regex = new Regex("%release.\S+%");
var match = r.Match(htmlText);
while (match.Success)
{
var value = match.Value;
var memberName = ParseMemberName(value); //Some code you write to parse out the member name from the match value
var propertyInfo = release.GetType().GetProperty(memberName);
var memberValue = propertyInfo.GetValue(release, null);
htmlText = htmlText.Replace(value, memberValue != null ? memberValue.ToString() : string.Empty);
match = match.NextMatch();
}
This is a talor made Probel for a preprocessed t4 template
You can have your help preformated in the template and allow the template engine to do the replacement. A small example below.
<div>
Please read the Cruise Control Details Below<br>
<br>
<table width='100%'>
<tr>
<td width='100%' colspan='5'>
<font size='4'><b>Release Details</b></font>
</td>
</tr>
<tr>
<td width='20%'>
<b>RFC Ticket #</b>
</td>
<td>
<#= RCFTicketVariable #>
</td>
You can use the Apache Velocity Engine port to .Net to do the templating for you
http://velocity.apache.org/engine/
http://velocity.apache.org/engine/devel/user-guide.html
http://nvelocity.sourceforge.net/
I would consider using REGEX (regular expressions) and giving the placeholders some sort of a special tag (ex: ) so you loop for all the tags that begin with .
Then you fill your data with a list or datatable and do 1 single loop for the whole replaces.
check these for help:
http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx
http://www.regular-expressions.info/examples.html (ur exact case is mentioned under Grabbing HTML Tags)

Categories

Resources