2014-10-22 58 views
0

拆分HTML字符串有一个HTML字符串,如下图所示我如何通过一段

<p style="font-family: Times New Roman;"><span class="cs5efed22f1">HARRISBURG - gov today issued a consumer alert about an apparent scam concerning the Affordable Care Act. In recent days, people across the country have received calls asking for personal information, in order to obtain a "national medical card" from the government. No such card exists.</span> 
</p> 
<p style="font-family: Times New Roman;"><span class="cs5efed22f1">"Do not ever give out personal, health care or bank account information over the phone, unless you initiated the call and you know who is on the other end," said Kane.</span> 
</p> 
<p style="font-family: Times New Roman;"><span class="cs5efed22f1">Kane says if someone calls, offering to help you obtain this nonexistent national medical card, you should call the AG's Health Care Hotline at (877) 888-4877. Any information you can provide-- such as the name, telephone number, or location of the caller-- will be very helpful.</span> 
</p> 
<p ><span class="cs5efed22f1"><span style="font-family: Times New Roman;">You can learn more about this (and other scams) by visiting the gov's website at</span> 
    <a href="http://www.example.org/"><span class="cs61a9f7db1" style="color: rgb(0, 0, 255); font-family: Times New Roman;">http://www.example.org</span> 
    </a><span style="font-family: Times New Roman;">.</span></span> 
</p> 
<p ><span class="cs5efed22f1"><span style="font-family: Times New Roman;">The Health Care Section of the test General's website can be found here:</span> 
    <a href="http://www.example.org/complaints.aspx?id=458"><span class="cs61a9f7db1" style="color: rgb(0, 0, 255); font-family: Times New Roman;">http://www.example.org/complaints.aspx?id=458</span> 
    </a> 
    </span> 
</p> 

是需要把一些额外的HTML通过C#中的第一段之后。

如何根据p标记拆分HTML字符串,并在第一段或第二段之后插入更多HTML?

+2

使用[HtmlAgilityPack(http://htmlagilitypack.codeplex.com/)这个指标。 – 2014-10-22 11:33:07

+0

我不需要使用html敏捷包吗?有没有办法通过regx或c#中的任何其他方式来做到这一点? – 2014-10-22 12:11:14

回答

1

尝试使用yourHtmlString.IndexOf("</p>"),这将给你的</p>中第一次出现的索引,然后使用内部的yourHtmlString.Insert()

+0

但是'

'或'

“以及所有其他变体呢?您在真实世界中看到的HTML可能非常混乱。 – 2014-10-22 12:49:28

+0

@JimMischel:他只是说,他想在第一段之后输入内容。让我们不要使事情复杂化,这很简单,如果需要,我们可以稍后再扩展。 ;) – ojek 2014-10-27 10:29:36

+0

但是他的例子清楚地显示了'

'和'

'。并发症已经存在。 – 2014-10-27 12:38:11