【问题标题】:Format and Replace <TR> and <TD> within an ArrayList and get a formatted ArrayList格式化和替换 ArrayList 中的 <TR> 和 <TD> 并获取格式化的 ArrayList
【发布时间】:2013-01-16 00:35:43
【问题描述】:

由于我无法格式化 TD 和 TR 的 HTML 格式(因为 StackOverflow.com 有一个自动 HTML 格式化程序,这会导致编写确切代码的问题,例如 TD 和 TR),所以请考虑以下格式建议的文本:-

<tr>
   <td>FO Special Added (Comments, Part #, RFQ #) - 13, 13, 13</td>
</tr>
<tr>
   <td>FO Special Added (Comments, Part #, RFQ #) - 14, 14, 14</td>
</tr>

我有一个从 Session 变量填充的 ArrayList:-
ArrayList ar3 = (ArrayList)Session["arr3"];

Session["arr3"] = "<tr> <td>FO Special Added (Comments, Part #, RFQ #) - 13, 13, 13</td> </tr><tr> <td>FO Special Added (Comments, Part #, RFQ #) - 14, 14, 14</td> </tr><tr> <td>FO Special Added (Comments, Part #, RFQ #) - 15, 15, 15</td> </tr> <td>FO Special Added (Comments, Part #, RFQ #) - 16, 16, 16</td> </tr>



我想要做的就是格式化这个 ArrayList(ar3),这样我可以在另一个 ArrayList(例如 ResultArList)中拥有从开始 TR & TD 到结束 TR & TD 的各个记录。
然后,此 ResultArList 将用于电子邮件中继和一些纯文本日志(在数据库中创建的自定义表)。

ResultArList 的记录应为:-
ResultArList[0]=FO 特别添加(评论、零件编号、RFQ #)- 13、13、13
ResultArList[1]=FO 特别添加(评论、零件编号、RFQ #)- 14、14、14
ResultArList[2]=FO 特别添加(评论、零件编号、RFQ #)- 15、15、15
ResultArList[3]=FO 特别添加(评论、零件编号、RFQ #) - 16、16、16

ArrayList ar3 中这些值的数量是无限的,但上述 HTML 的格式是不变的。

请帮助我如何自定义格式我的 ArrayList 并创建一个新的格式化 ArrayList。

【问题讨论】:

  • 花几分钟学习如何格式化代码。您的问题无法阅读。
  • 您需要将您的代码格式化为代码,这样它就不会呈现为 HTML。有了你所拥有的,它已经被破坏得太严重了,甚至无法阅读。
  • 你真的不应该在 C# 中使用ArrayList;你应该使用通用的List

标签: c# asp.net arraylist html-parsing


【解决方案1】:

如果我正确理解您的问题,您可以使用正则表达式提取标签之间的任何内容;例如:

var regex = new System.Text.RegularExpressions.Regex(@"(?<=<td>).+?(?=</td>)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
var text = "<tr> <td>FO Special Added (Comments, Part #, RFQ #) - 13, 13, 13</td> </tr><tr> <td>FO Special Added (Comments, Part #, RFQ #) - 14, 14, 14</td> </tr><tr> <td>FO Special Added (Comments, Part #, RFQ #) - 15, 15, 15</td> </tr> <td>FO Special Added (Comments, Part #, RFQ #) - 16, 16, 16</td> </tr>";
var matches = regex.Matches(text);

var results = matches.Cast<Match>()
    .Select(match => match.Value)
    .ToList();

【讨论】:

  • 杰罗德!非常感谢您的解决方案,它解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 2012-02-25
  • 2018-02-09
相关资源
最近更新 更多