How to apply Style Css in mvc foreach loop - c#

I'm new in asp.net mvc and My problem is, I want to conditionally add a CSS background-color to a set of table rows, based on BILL-AMOUNT = 0.00 i have code like this
#model IEnumerable
View
<tbody>
#foreach (var item in Model)
{
<tr>
<td style="padding-left: 12px;">#Html.DisplayFor(modelItem => item.TicketNo)</td>
<td style="padding-left: 12px; display: none;">#Html.DisplayFor(modelItem => item.name)</td>
<td style="padding-left: 12px;">#Html.DisplayFor(modelItem => item.ArrDate)</td>
<td style="padding-left: 12px;">#Html.DisplayFor(modelItem => item.quantity)</td>
<td style="padding-left: 12px;">#Html.DisplayFor(modelItem => item.Total)</td>
<td>#Html.DisplayFor(modelItem => item.Refund)</td>
<td>#Html.DisplayFor(modelItem => item.TotalAmount)</td>
</tr>
}
</tbody>

Assuming you have a class:
.tr-zero {
background-color: #cccccc;
}
You can add the class conditionally like this:
<tr#(item.TotalAmount == 0 ? " class=\"tr-zero\"" : string.Empty)>

<tbody>
#foreach (var item in Model)
{
<tr bgcolor='#(item.AMOUNT==0? "#FFF":"#EEE")'>
<td style="padding-left: 12px;">#Html.DisplayFor(modelItem => item.TicketNo)</td>
<td style="padding-left: 12px; display: none;">#Html.DisplayFor(modelItem => item.name)</td>
<td style="padding-left: 12px;">#Html.DisplayFor(modelItem => item.ArrDate)</td>
<td style="padding-left: 12px;">#Html.DisplayFor(modelItem => item.quantity)</td>
<td style="padding-left: 12px;">#Html.DisplayFor(modelItem => item.Total)</td>
<td>#Html.DisplayFor(modelItem => item.Refund)</td>
<td>#Html.DisplayFor(modelItem => item.TotalAmount)</td>
</tr>
}

<tbody>
#foreach (var item in Model)
{
<tr style="background-color:#(Convert.ToInt32(item.TotalAmount) == 0?"RedColor":"GreenColor")">
<td style="padding-left: 12px;">#Html.DisplayFor(modelItem => item.TicketNo)</td>
<td style="padding-left: 12px; display: none;">#Html.DisplayFor(modelItem => item.name)</td>
<td style="padding-left: 12px;">#Html.DisplayFor(modelItem => item.ArrDate)</td>
<td style="padding-left: 12px;">#Html.DisplayFor(modelItem => item.quantity)</td>
<td style="padding-left: 12px;">#Html.DisplayFor(modelItem => item.Total)</td>
<td>#Html.DisplayFor(modelItem => item.Refund)</td>
<td>#Html.DisplayFor(modelItem => item.TotalAmount)</td>
</tr>
}
</tbody>

Related

Calculate age in Razor(ASP.NET mvc)

I have a View with a table
Here is the code of the View:
#foreach (var item in Model)
{
<tr>
<td class="point">
#(rowNo += 1)
</td>
<td class="FIO" style="text-align: center; font-size: 16px; cursor:pointer">#Html.DisplayFor(modelItem => item.FIO) #Html.Hidden("clientEmail", item.FIO)</td>
<td style="text-align: center; font-size: 16px;">
#Html.DisplayFor(modelItem => item.Email)
</td>
<td style="text-align: center; font-size: 16px;">
#Html.DisplayFor(modelItem => item.City)
</td>
<td style="text-align: center; font-size: 16px;">
#Html.DisplayFor(modelItem => item.Birthday)
</td>
<td style="text-align: center; font-size: 16px;">
#Html.DisplayFor(modelItem => item.Salary)
</td>
<td style="text-align: center; font-size: 16px;">
#Html.DisplayFor(modelItem => item.English)
</td>
<td style="text-align: end;">
<a href='#Url.Action("Edit", "Interwier", new {id = item.Interwier_id})'>
<img src='#Url.Content("~/Images/Edit.png")' />
</a>
<a href='#Url.Action("Delete", "Interwier", new {id = item.Interwier_id})'>
<img src='#Url.Content("~/Images/Delete.png")' />
</a>
</td>
</tr>
}
This code is about Age:
<td style="text-align: center; font-size: 16px;">
#Html.DisplayFor(modelItem => item.Birthday)
</td>
It returns me year from database. I need to show age.
How I can do this correctly?
By your description, your Birthday is a year value you can:
first add a new property in your model used in view for generating Age:
public class ModelName
{
// other properties
// original Birthday property
public int Birthday {get;set;}
// get age by subtracting current year with Birthday year.
public int Aget {get {return DateTime.Now.Year - Birthday;}}
}
then in your view
change displaying Birthday to Age
<td style="text-align: center; font-size: 16px;">
#Html.DisplayFor(modelItem => item.Age)
</td>

Scrollable list

I have table with data that I display at table
Here is Controller
public ActionResult Index()
{
return View(db.Companies.ToList());
}
Here is View
#foreach (var item in Model)
{
<tr>
<td class="point">
#(rowNo += 1)
</td>
<td class="title" style="text-align: center; font-size:20px">
#Html.DisplayFor(modelItem => item.CompanyName)
</td>
<td class="title" style="text-align: center; font-size:20px">
#Html.DisplayFor(modelItem => item.Vacancies.FirstOrDefault().VacancyName)
</td>
<td style="text-align: end;">
<a href='#Url.Action("Edit", "Companies", new {id = item.CompanyID})'>
<img src='#Url.Content("~/Images/Edit.png")'/>
</a>
<a href='#Url.Action("Delete", "Companies", new {id = item.CompanyID})'>
<img src='#Url.Content("~/Images/Delete.png")'/>
</a>
</td>
</tr>
}
Vacancies is Related Data Table to Companies
Company may have 1-10 Vacancies.
How I can show all Vacancies via scrollable list here <td class="title" style="text-align: center; font-size:20px">
#Html.DisplayFor(modelItem => item.Vacancies.FirstOrDefault().VacancyName)
</td>
Assuming that Vacancies is an IEnumerable, or similar collection object (probably is given your use of FirstOrDefault), you could simply iterate them, much the same way you're iterating the Companies list:
#foreach (var item in Model)
{
<tr>
<td class="point">
#(rowNo += 1)
</td>
<td class="title" style="text-align: center; font-size:20px">
#Html.DisplayFor(modelItem => item.CompanyName)
</td>
<td class="title" style="text-align: center; font-size:20px">
<div style="height: 100px; overflow-y: scroll">
#foreach (var vacancy in item.Vacancies)
{
<div>#Html.DisplayFor(v => vacancy.VacancyName)</div>
}
</div>
</td>
<td style="text-align: end;">
<a href='#Url.Action("Edit", "Companies", new {id = item.CompanyID})'>
<img src='#Url.Content("~/Images/Edit.png")'/>
</a>
<a href='#Url.Action("Delete", "Companies", new {id = item.CompanyID})'>
<img src='#Url.Content("~/Images/Delete.png")'/>
</a>
</td>
</tr>
}
On an unrelated note, you could, and arguably should, stick to double quotes for HTML attributes:
<img src="#Url.Content("~/Images/Edit.png")" />

Generate pdf using iTextSharp for different languages

I have tried different options, but it did not work. The code generated pdf for English but does not work for other languages.
using (var ms = new MemoryStream())
{
// Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
var doc = new Document();
{
// Create a writer that's bound to our PDF abstraction and our stream
var writer = PdfWriter.GetInstance(doc, ms);
{
// Open the document for writing
doc.Open();
string finalHtml = string.Empty;
// Read your html by database or file here and store it into finalHtml e.g. a string
// XMLWorker also reads from a TextReader and not directly from a string
using (var srHtml = new StringReader(sHtmlText))
{
// Parse the HTML
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
}
doc.Close();
}
}
// After all of the PDF "stuff" above is done and closed but **before** we
// close the MemoryStream, grab all of the active bytes from the stream
return new PDFFormFillerResult(ms, PDFFormFillerResultType.Success, string.Empty);
//bytes = ms.ToArray();
}
Well we had to buy another 3rd party tool which understands UNICODE characters.
I was able to create multi language PDF in English and Japanese.
The prerequisite for achieving this
1. To have appropriate Font which supports the languages you are planning to use.
2. Implement IFontProvider interface from iTextSharp and register the multi language font you will be using.
If you follow these 2 steps, you can create multi language PDF.
Code Sample:
public class smartUIFontProvider: IFontProvider
{
public Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color)
{
string myFont = #"C:\Tasks\Projects\SampleProject\iTextDemo_PDF\SmartFontUI\SmartFontUI.otf";
iTextSharp.text.pdf.BaseFont bfR;
bfR = iTextSharp.text.pdf.BaseFont.CreateFont(myFont,
iTextSharp.text.pdf.BaseFont.IDENTITY_H,
iTextSharp.text.pdf.BaseFont.EMBEDDED);
iTextSharp.text.BaseColor clrBlack =
new iTextSharp.text.BaseColor(0, 0, 0);
iTextSharp.text.Font fntHead =
new iTextSharp.text.Font(bfR, 12, iTextSharp.text.Font.NORMAL, clrBlack);
return fntHead;
}
public bool IsRegistered(string fontname)
{
return true;
}
}
static void Main (string[] args)
{
// step 1
Document document = new Document();
Byte[] bytes;
var fileName = "resources\\test_" + System.DateTime.Now.ToString("ddMMyyyy_hhmm") + ".pdf";
var testFile = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), fileName);
MemoryStream ms = new MemoryStream();
// step 2
using (var writer = PdfWriter.GetInstance(document, ms))
{
document.Open();
String example_html = #" <!DOCTYPE html> <html> <head> </head> <body> <h1>Change of Control Application</h1> <br/> <p>Please fill out this form if there are updates to your current organization and its relationship Company has on file with the legal binding entity. Company will review the information and may ask you to provide further information before Company agrees to the requested changes.</p> <br/> <table class='bodyText1LIC general' style='width:700px' id='tblInputForm' > <tr> <td class='bodyText1LIC' colspan='4'> </td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Licensee Information</h3> </td> </tr> <tr> <td class='bodyText1SerNo'>1.</td> <td class='bodyText1LIC'>Licensee Name </td> <td valign='top' class='txtboxtd'> 字詰めなどの調整をおすすめします </td> <td> </td> </tr> <tr> <td class='bodyText1LIC' colspan='4'> </td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Licensee Primary Contact Information </h3> </td> </tr> <tr> <td class='bodyText1SerNo'>2.</td> <td class='bodyText1LIC'>Name</td> <td class='txtboxtd'> めなどの調整 </td> <td> </td> </tr> <tr> <td class='bodyText1SerNo'>3.</td> <td class='bodyText1LIC'>Job Title</td> <td class='txtboxtd'> T</td> <td> </td> </tr> <tr> <td class='bodyText1SerNo'>4.</td> <td class='bodyText1LIC'>Mailing Address</td> <td class='txtboxtd'>めなどの調整</td> <td> </td> </tr> <tr> <td class='bodyText1SerNo'>5.</td> <td class='bodyText1LIC'>Telephone Number </td> <td class='txtboxtd'>T</td> <td> </td> </tr> <tr> <td class='bodyText1SerNo'>6.</td> <td class='bodyText1LIC'>Email Address </td> <td class='txtboxtd'>abc#test.com</td> <td> </td> </tr> <tr> <td class='bodyText1LIC' colspan='4'> </td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3> Organizational Structure</h3> </td> </tr> <tr> <td class='bodyText1SerNo'> 7.</td> <td class='bodyText1LIC' colspan='3'> Will your Company's Name Change? </td> </tr> <tr> <td></td> <td colspan='3'> Yes</td> </tr> <tr class='nc' id='TrCompanyName'> <td></td> <td class='bodyText1LIC' colspan='2'> a) New company name </td> <td class='txtboxtd'> DSFDASFASDFDSA</td> </tr> <tr class='nc' id='TrEffectiveDate'> <td></td> <td class='bodyText1LIC' colspan='2'> b) Effective Date Company change will take place </td> <td class='txtboxtd'> 04/20/2017</td> </tr> <tr> <td class='bodyText1SerNo'>8.</td> <td class='bodyText1LIC' colspan='3'>Has your Company been Reincorporated or under gone an Internal Reorganization? </td> </tr> <tr> <td></td> <td colspan='3'> No</td> </tr> <tr> <td class='bodyText1LIC' colspan='4'> </td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Assets / Stocks to be Acquired </h3> </td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Assets to be Acquired</h3> </td> </tr> <tr> <td class='bodyText1SerNo'>9.</td> <td class='bodyText1LIC' colspan='3'>Will some or all of your Company’s Assets be Acquired? </td> </tr> <tr> <td></td> <td colspan='3'> No</td> </tr> <tr> <td class='bodyText1LIC' colspan='4'> </td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Stocks to be Acquired </h3> </td> </tr> <tr> <td class='bodyText1SerNo'>10.</td> <td class='bodyText1LIC' colspan='3'>Will some or all of your Company’s Assets be Acquired? </td> </tr> <tr> <td></td> <td colspan='3'> No</td> </tr> <tr> <td class='bodyText1LIC' colspan='4'> </td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3> Effects of Change to Licensee</h3> </td> </tr> <tr> <td class='bodyText1SerNo'> 11.</td> <td class='bodyText1LIC' colspan='3'> Please indicate which Company is the Surviving Legal Entity? </td> </tr> <tr> <td></td> <td class='txtboxtd' colspan='3'> T</td> </tr> <tr> <td class='bodyText1SerNo'>12.</td> <td class='bodyText1LIC' colspan='3'>Please provide the Company that will be the Licensee? </td> </tr> <tr> <td class='txtboxtd'> </td> <td colspan='3'> T</td> </tr> <tr> <td valign='top' class='bodyText1SerNo'>13.</td> <td class='bodyText1LIC' colspan='3'>Will any companies exist to operate as separate Subsidiaries or Affiliates ? </td> </tr> <tr> <td></td> <td colspan='3'> No</td> </tr> <tr> <td class='bodyText1LIC' colspan='4'> </td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Existing Agreements</h3> </td> </tr> <tr> <td class='bodyText1SerNo'>14.</td> <td class='bodyText1LIC' colspan='3'>Please identify if any Licenses that will be affected by the Changes described above </td> </tr> <tr> <td></td> <td colspan='3'> No</td> </tr> <tr> <td class='bodyText1LIC' colspan='4'> </td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Corporate Documentation</h3> </td> </tr> <tr> <td valign='top' class='bodyText1SerNo'>15.</td> <td class='bodyText1LIC' colspan='3'>Please Select from 1 of the 2 options listed below</td> </tr> <tr> <td></td> <td colspan='3'> Please Describe the Full Legal Structure of this Organization Change in relation to Licensing.</td> </tr> <tr> <td></td> <td colspan='3'> ASDFSDAFDSAFADSFDASF </td> </tr> </table> </body> </html>";
String example_css = #"html { font-family: SmartFontUI; font-size: 14px; } h1, h2, strong { font-family: SmartFontUI; font-weight: bold; display: inline; } h1 { font-size: 18pt; } .main { font-size: 12pt; color: black; font-family: SmartFontUI, Arial, Sans-Serif; background-color: white; text-align: left; line-height: 1.4em; margin: 2%; } .mainDesc { width: 100%; margin: 10px 0; } span { float: left; display: block; width: 100%; margin-bottom: 5px; } .bodyText1SerNo { width: 2em; } .bodyText1LIC { width: 22em; } .txtboxspan { width: 32em; } .tableBody1 { width: 100%; } .nc { margin-left: 100px; } .subtitle { font-size: 16pt; }";
FontFactory.Register(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "SmartFontUI.otf"), "SmartFontUI");
XMLWorkerHelper worker = XMLWorkerHelper.GetInstance();
MemoryStream msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html));
MemoryStream msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css));
smartUIFontProvider smartUIFontObj = new smartUIFontProvider();
worker.ParseXHtml(writer, document, msHtml, msCss, System.Text.Encoding.UTF8, smartUIFontObj);
// step 5
document.Close();
}
bytes = ms.ToArray();
System.IO.File.WriteAllBytes(testFile, bytes);
}

collapse panel in view page is showing first record on all button

<table class="table" >
#foreach (var item in Model)
{
<tr>
<td>
<a class="btn" data-toggle="collapse" data-target="#viewdetails_#item.Title">
#Html.DisplayFor(modelItem => item.Title)
</a>
<div class="collapse" id="viewdetails_#item.Title">
<table>
<tr>
<th>#Html.DisplayNameFor(model => model.Category)</th>
<td>#Html.DisplayFor(modelItem => item.Category)</td>
</tr>
<tr>
<th>#Html.DisplayNameFor(model => model.Budget)</th>
<td>#Html.DisplayFor(modelItem => item.Budget)</td>
</tr>
<tr>
<th>#Html.DisplayNameFor(model => model.EstimatedHours)</th>
<td>#Html.DisplayFor(modelItem => item.EstimatedHours)</td>
</tr>
<tr>
<th>#Html.DisplayNameFor(model => model.SuggestedSources)</th>
<td>#Html.DisplayFor(modelItem => item.SuggestedSources)</td>
</tr>
<tr>
<th>#Html.DisplayNameFor(model => model.ProposedDeadline)</th>
<td>#Html.DisplayFor(modelItem => item.ProposedDeadline)</td>
</tr>
<tr>
<th>#Html.DisplayNameFor(model => model.Details)</th>
<td>#Html.DisplayFor(modelItem => item.Details)</td>
</tr>
<tr>
<th>#Html.DisplayNameFor(model => model.Comments)</th>
<td>#Html.DisplayFor(modelItem => item.Comments)</td>
</tr>
<tr>
<th>#Html.DisplayNameFor(model => model.Status)</th>
<td>#Html.DisplayFor(modelItem => item.Status)</td>
</tr>
</table>
</div>
</td>
<td>
#Html.DisplayFor(modelItem => item.Category)
</td>
<td>
#Html.DisplayFor(modelItem => item.Budget)
</td>
<td>
#Html.DisplayFor(modelItem => item.Details)
</td>
<td>
#Html.DisplayFor(modelItem => item.Status)
</td>
</tr>
}
</table>
/* The above is my code. I want a collapse on title click. There are several titles, but on every title click the collapse window shows me first record. What should I do to show respective title record.*/
Your are passing same id in for loop to the elements that is wrong, in your page everty anchor tag will have data-target viewdetails due to which it will always open the first div with viewdetials id which browser will find in the DOM
do like this:
<a class="btn" data-toggle="collapse" data-target="#viewdetails_#item.Id" onclick="">
and in div:
<div class="collapse" id="viewdetails_#item.Id">
or you can use IndexOf method of the list item and pass that with the ids:
and in div:
<div class="collapse" id="viewdetails_#Model.IndexOf(item)">

Getting correct XPATH expression for DIV CLASS

i am trying to traverse an html document:
<body class="style_0">
<div>
<div class="style_1">Pending Test List</div>
<table style=" width: 100%;" id="AUTOGENBOOKMARK_4365445353431356880">
<col>
<col>
<tbody>
<tr>
<td style="vertical-align: baseline;">
<div class="style_4">Pending Test List</div>
</td>
<td style="vertical-align: baseline;">
<div class="style_5">SOME AGENCY Laboratories, Inc.</div>
</td>
</tr>
</tbody>
</table>
<table class="style_6" style=" width: 4.531in;" id="AUTOGENBOOKMARK_5083738604442918131">
<col style=" width: 1in;">
<col class="style_7" style=" width: 0.75in;">
<col class="style_8" style=" width: 0.6in;">
<col style=" width: 0.75in;">
<col style=" width: 2.375in;">
<tbody>
<tr class="style_9" style=" height: 0.5in;">
<td style="vertical-align: middle;">
<div class="style_10">Report Range:</div>
</td>
<td style="vertical-align: middle;">
<div class="style_11">01/01/2012</div>
</td>
<td style="vertical-align: middle;">
<div class="style_12">through</div>
</td>
<td style="vertical-align: middle;">
<div class="style_13">01/31/2012</div>
</td>
<td style="vertical-align: middle;">
<div class="style_14">(by Date Entered)</div>
</td>
</tr>
</tbody>
</table>
<table class="style_15" style=" width: 100%;" id="AUTOGENBOOKMARK_7602283385844673591" iid="/526
(QuRs78576248:0)">
<col style=" width: 0.75in;">
<col style=" width: 1.25in;">
<col style=" width: 1in;">
<col style=" width: 1.5in;">
<col style=" width: 1.5in;">
<col style=" width: 1.5in;">
<col>
<thead>
<tr>
<td colspan="4" style="vertical-align: baseline;"></td>
<td style="vertical-align: baseline;"></td>
<td style="vertical-align: baseline;"></td>
<td style="vertical-align: baseline;"></td>
</tr>
<tr>
<td style="vertical-align: baseline;">
<div class="style_16">Entered</div>
</td>
<td style="vertical-align: baseline;">
<div class="style_16">Spec. ID</div>
</td>
<td style="vertical-align: baseline;">
<div class="style_16">Batch/Pos.</div>
</td>
<td style="vertical-align: baseline;">
<div class="style_16">Test</div>
</td>
<td style="vertical-align: baseline;">
<div class="style_16">Client ID</div>
</td>
<td style="vertical-align: baseline;">
<div class="style_16">Client Name</div>
</td>
<td style="vertical-align: baseline;">
<div class="style_16">Agency</div>
</td>
</tr>
</thead>
<tbody>
<tr>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_18">1/30/12</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_19">ZZ324sdf</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_18">51446 / 75</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">HOLD_DE</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">234234</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">smith, john</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">PPPM-6P - SOME AGENCY</div>
</td>
</tr>
<tr>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_18">1/31/12</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_19">SFD3434</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_18">51668 / 17</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">HOLD_DE</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">FOY, EL</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">FOY, ALEX</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">someagency & Associates LLC</div>
</td>
</tr>
<tr>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_18">1/31/12</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_19">SFD3434</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_18">51668 / 25</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">HOLD_DE</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">JAMISON, PA</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">JAMISON, ROY</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">someagency & Associates LLC</div>
</td>
</tr>
<tr>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_18">1/31/12</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_19">SFD3434</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_18">51669 / 34</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">HOLD_DE</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">NEWMAN, SO</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">NEWMAN, ALEX</div>
</td>
<td class="style_17" style="vertical-align: baseline;">
<div class="style_20">someagency & Associates LLC</div>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2" style="vertical-align: baseline;">
<div class="style_21">Total Tests:</div>
</td>
<td style="vertical-align: baseline;">
<div class="style_22">4</div>
</td>
<td style="vertical-align: baseline;"></td>
<td style="vertical-align: baseline;"></td>
<td style="vertical-align: baseline;"></td>
<td style="vertical-align: baseline;"></td>
</tr>
</tfoot>
</table>
<table style=" width: 100%;" id="AUTOGENBOOKMARK_8507236727661888074">
<col>
<col>
<col>
<tbody>
<tr>
<td style="vertical-align: baseline;">
<div class="style_2">
<br>Feb 13, 2012 9:37 AM</div>
</td>
<td style="vertical-align: baseline;">
<div class="style_3">
<br>
<div style="text-align:center;">Page 1</div>
</div>
</td>
<td style="vertical-align: baseline;"></td>
</tr>
</tbody>
</table>
</div>
</body>
for this data:
so far i have this:
foreach (var row in htmlSnippet.DocumentNode.SelectNodes("//table[#class = 'style_15']/tbody/tr"))
{
foreach (var cell in row.SelectNodes("div[#class='*']"))
{
textBox1.Text = cell.InnerHtml.ToString();
}
}
however i am not returning anything!
this line is working:
//table[#class = 'style_15']/tbody/tr
but this does not return anthing:
("div[#class='*']"))
please let me know what i am doing wrong! i need help returning every piece of data as shown in the image (except for the field names)
The * is typically used for matching any element or attribute name, not any value. If you want to match all div elements having a class attribute with any value, just use #class.
foreach (var row in htmlSnippet.DocumentNode.SelectNodes("//table[#class = 'style_15']/tbody/tr/td"))
{
foreach (var cell in row.SelectNodes("div[#class]"))
{
textBox1.Text = cell.InnerHtml.ToString();
}
}
You probably want simply div[#class]—a div element that has a class attribute.
Oh, it's also worth noting that the HTML/XML sample you provided is not well-formed. I had to remove all the col elements, and close the br elements. Maybe, for C#, that's an issue... I know it is for XSL in general... not sure about XPath.
I don't have time to code up a C# example, but here's a simple XSL:
<xsl:template match="/">
<so>
<xsl:apply-templates select="//table[#class = 'style_15']/tbody/tr"/>
</so>
</xsl:template>
<xsl:template match="div[#class]">
<xsl:copy-of select="."/>
</xsl:template>
I get this output:
<so>
<div class="style_18">1/30/12</div>
<div class="style_19">ZZ324sdf</div>
<div class="style_18">51446 / 75</div>
<div class="style_20">HOLD_DE</div>
<div class="style_20">234234</div>
<div class="style_20">smith, john</div>
<div class="style_20">PPPM-6P - SOME AGENCY</div>
<div class="style_18">1/31/12</div>
<div class="style_19">SFD3434</div>
<div class="style_18">51668 / 17</div>
<div class="style_20">HOLD_DE</div>
<div class="style_20">FOY, EL</div>
<div class="style_20">FOY, ALEX</div>
<div class="style_20">someagency & Associates LLC</div>
<div class="style_18">1/31/12</div>
<div class="style_19">SFD3434</div>
<div class="style_18">51668 / 25</div>
<div class="style_20">HOLD_DE</div>
<div class="style_20">JAMISON, PA</div>
<div class="style_20">JAMISON, ROY</div>
<div class="style_20">someagency & Associates LLC</div>
<div class="style_18">1/31/12</div>
<div class="style_19">SFD3434</div>
<div class="style_18">51669 / 34</div>
<div class="style_20">HOLD_DE</div>
<div class="style_20">NEWMAN, SO</div>
<div class="style_20">NEWMAN, ALEX</div>
<div class="style_20">someagency & Associates LLC</div>
</so>
This is just an intermediate output to show that the XPath is working correctly.
Hope this helps.

Categories