通配符或字符串可以“包含”吗?可以用在 switch {case} 中吗?注意:罗嗦
作为编程新手,我阅读了大量示例代码,并尝试将它们组合在一起,以了解哪些内容有效。我正在使用 html 敏捷包尝试废弃新闻网页。
问题:我正在测试的节点之一不使用静态值,它使用查看时间。我如何将其应用于 switch {case} 方法。如果我在整个方法中偏离了基础,我也愿意接受任何建议。
另请注意:如果有一种适合我的方法可以跳过它,我不需要捕获此节点。
我决定使用一个使用开关的 exapmle,
var rows = doc.DocumentNode.SelectNodes(".//*[@id='weekdays']/tr");
foreach (var row in rows)
{
var cells = row.SelectNodes("./td");
string title = cells[0].InnerText;
var valueRow = cells[2];
switch (title)
{
case "Date":
HtmlNode date = valueRow.SelectSingleNode("//*[starts-with(@id, 'detail_row_seek')]/td");
Console.WriteLine("UPC=A:\t" + date.InnerText);
break;
case "":
string Time = valueRow.InnerText;
Console.WriteLine("Time:\t" + Time);
break;
case "News":
string Time = valueRow.InnerText;
Console.WriteLine("News:\t" + News);
break;
}
html 摘录
<table id="weekdays" cellpadding="6" cellspacing="0" border="0" width="100%">
<tr>
<td class="thead" style="border-bottom: 1px solid #d1d1e1;font-weight:normal; text-align: center; width:8%; padding-left: 6px;">Date</td>
<td class="thead" style="border-bottom: 1px solid #d1d1e1;font-weight:normal; width:8%; text-align: center; white-space:nowrap"><a href="guestcp.php?do=customoptions" title="Time & Date Options"><img style="position:relative; vertical-align: bottom;" src="images/misc/clock_small.gif" title="Time & Date Options" alt="Time & Date Options" border="0" /></a><a href="guestcp.php?do=customoptions" title="Time & Date Options"><span id="ff_nowtime_clock">3:20pm</span></a></td>
<td class="thead" style="border-bottom: 1px solid #d1d1e1;font-weight:normal; text-align: center; width:8%;">News</td>
......
<tr id="detail_row_seek_37876">
<td id="toprow_9" class="alt1 espace" rowspan="3" style="vertical-align: top; text-align: center;" nowrap="nowrap">
<span class="smallfont">
<div>Sat</div>
Apr 9
</span>
</td>
<td class="alt1 espace" style="text-align: center;" nowrap="nowrap">
<span class="smallfont">Day 3</span>
</td>
<td class="alt1 espace" style="text-align: center;"><span class="smallfont">EUR</span></td>
<td class="alt1 espace" style="padding-top: 2px" align="center">
<a name="chart=37876" style="position:absolute; margin-top: -10px;"></a><a name="details=37876" style="position:absolute; margin-top: -10px;"></a>
<div class="cal_imp_medium" title="Medium Impact Expected"></div></td>
<td class="alt1 espace">
<div class="smallfont" id="title_37876" style="padding-left: 11px;">ECOFIN Meetings</div>
</td>
问题是:所谓的时间列不是静态的,它实际使用时间值。有没有办法在案例中使用通配符或执行“包含”来解决这个非常冗长的问题?
Being new to programming I read a lot of sample code and try to hack things together in an attempt to learn what works. I am working with html agility pack trying to scrap a news webpage.
Problem: One of the nodes that I am testing does not use a static value, it uses time of viewing. How can I apply this to a switch {case} method. I am also open for any suggestions if I am way off base in this entire approach.
Note also: I don't need to capture this node, if there is a way to skip it that works for me.
I decided to use an exapmle that uses a switch,
var rows = doc.DocumentNode.SelectNodes(".//*[@id='weekdays']/tr");
foreach (var row in rows)
{
var cells = row.SelectNodes("./td");
string title = cells[0].InnerText;
var valueRow = cells[2];
switch (title)
{
case "Date":
HtmlNode date = valueRow.SelectSingleNode("//*[starts-with(@id, 'detail_row_seek')]/td");
Console.WriteLine("UPC=A:\t" + date.InnerText);
break;
case "":
string Time = valueRow.InnerText;
Console.WriteLine("Time:\t" + Time);
break;
case "News":
string Time = valueRow.InnerText;
Console.WriteLine("News:\t" + News);
break;
}
excerpt of html
<table id="weekdays" cellpadding="6" cellspacing="0" border="0" width="100%">
<tr>
<td class="thead" style="border-bottom: 1px solid #d1d1e1;font-weight:normal; text-align: center; width:8%; padding-left: 6px;">Date</td>
<td class="thead" style="border-bottom: 1px solid #d1d1e1;font-weight:normal; width:8%; text-align: center; white-space:nowrap"><a href="guestcp.php?do=customoptions" title="Time & Date Options"><img style="position:relative; vertical-align: bottom;" src="images/misc/clock_small.gif" title="Time & Date Options" alt="Time & Date Options" border="0" /></a><a href="guestcp.php?do=customoptions" title="Time & Date Options"><span id="ff_nowtime_clock">3:20pm</span></a></td>
<td class="thead" style="border-bottom: 1px solid #d1d1e1;font-weight:normal; text-align: center; width:8%;">News</td>
.........
<tr id="detail_row_seek_37876">
<td id="toprow_9" class="alt1 espace" rowspan="3" style="vertical-align: top; text-align: center;" nowrap="nowrap">
<span class="smallfont">
<div>Sat</div>
Apr 9
</span>
</td>
<td class="alt1 espace" style="text-align: center;" nowrap="nowrap">
<span class="smallfont">Day 3</span>
</td>
<td class="alt1 espace" style="text-align: center;"><span class="smallfont">EUR</span></td>
<td class="alt1 espace" style="padding-top: 2px" align="center">
<a name="chart=37876" style="position:absolute; margin-top: -10px;"></a><a name="details=37876" style="position:absolute; margin-top: -10px;"></a>
<div class="cal_imp_medium" title="Medium Impact Expected"></div></td>
<td class="alt1 espace">
<div class="smallfont" id="title_37876" style="padding-left: 11px;">ECOFIN Meetings</div>
</td>
The problem is: The so called time column is not static it actual uses a time value. Is there a way to use a wild card in the case or a way to do a "contains" to get around this very wordy problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须在 switch 语句的每种情况下使用常量值。
我能想到的让你做你想做的事情的唯一方法是使用
default:
情况 - 在此默认情况下,你可以使用测试你正在寻找的值包含使用
、if
的Parse
或Regex
测试。我无法完全遵循您的 HTML 示例代码(抱歉!) - 但修改后的 C# 可能看起来像这样:
You must use constant values in each case of the switch statement.
The only way I can think of for you to do what you are looking to do is to use the
default:
case - within this default case you can test the value you are looking for using acontains
,Parse
orRegex
test usingif
.I couldn't quite follow your HTML sample code (sorry!) - but the modified C# might look something like:
您可以使用“case default:”并在其中添加一个条件来进行检查。
You could use the "case default:" and put a condition inside that would check.
case 必须是常量表达式。请参阅 MSDN。如果您可以改用 if-else,您将拥有更多自由。
The case has to be a constant expression. see MSDN. If you can switch to using if-else, you'll have more freedom.
添加默认实现,并将额外的条件检查放在那里。或者,使用 if 语句可能会更好。
Add a default implementation, and put your extra condition checks there. Or you may be better off with just if statements.