c# - 从存储为字符串的 html 中输出前两段

标签 c# .net html string substring

我将 html 存储在我的 c# .net 2.0 代码中的一个字符串变量中。下面是一个例子:

<div class="track">
    <img alt="" src="http://hits.guardian.co.uk/b/ss/guardiangu-feeds/1/H.20.3/30561?ns=guardian&pageName=Hundreds+feared+dead+in+Haiti+quake%3AArticle%3A1336252&ch=World+news&c3=GU.co.uk&c4=Haiti+%28News%29%2CDominican+Republic+%28News%29%2CCuba+%28News%29%2CBahamas+%28News%29%2CNatural+disasters+and+extreme+weather+%28News%29%2CEnvironment%2CWorld+news&c6=Rory+Carroll%2CHaroon+Siddique&c7=10-Jan-13&c8=1336252&c9=Article&c10=News&c11=World+news&c13=&c25=&c30=content&h2=GU%2FWorld+news%2FHaiti" width="1" height="1" />
</div>
<p class="standfirst">
    • Tens of thousands lose homes in 7.0 magnitude quake<br />
    • UN headquarters, schools and hospitals collapse
</p>
<p>
    René Préval, the president of Haiti, has described the devastation after last night's earthquake as "unimaginable" as governments and aid agencies around the world rushed into action.
</p>
<p>
    Préval described how he had been forced to step over dead bodies and heard the cries of those trapped under the rubble of the national parliament. "Parliament has collapsed. The tax office has collapsed. Schools have collapsed. Hospitals have collapsed," <a href="http://www.miamiherald.com/582/story/1422279.html" title="he told the Miami Herald">he told the Miami Herald</a>. "There are a lot of schools that have a lot of dead people in them." Préval said he thought thousands of people had died in the quake.
</p>

我只想输出前两段作为原文的子串。

有人可以帮忙吗?

最佳答案

看看 Html Agility Pack .

它公开了一个非常强大的用于解析 HTML 的 API,可用于提取您想要的数据。

关于c# - 从存储为字符串的 html 中输出前两段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2058810/

相关文章:

c# - 无法找到 'app.xaml',IOException 直到修改 app.xaml

c# - 如何向托管 ISP 发出获取请求?

c# - 根据 C# 中的实例化类,利用不同的依赖项

.net - 为什么.Net框架中的每个类都没有对应的接口(interface)?

c# - WCF 中的 WCF 服务引用未正确获取方法类型

jQuery 在要添加到 DOM 的段落元素中添加一个 span 元素

c# - 将字典转换为 List<Customer>

javascript - 如何扫描 HTML 和 javascript 文件以实现跨浏览器兼容性?

javascript - 如何使用 JavaScript 将格式应用于 HTML 表格中的 <td>?

c# - 如何在 C# 中将 g729 编码字节数组转换为 .WAV?