通过ID获取div内容 [英] Get div content by id
本文介绍了通过ID获取div内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,可以保存整个html文档.我想在具有特定ID的div中获取所有内容.例如:
I have a string that keeps entire html document. I would like to get all the content inside a div with specific id. For example:
<div id="myId" class = "myClass">
<div class = "myClass">hello</div>
</div>
我需要id ="myId"标记与结束标记之间的内容. 有什么办法可以做到这一点?输出应为第二行.
I need the content between the tag with id="myId" and it's closing tag. Any way to achieve this? The output should be the second line.
推荐答案
The clean and correct way would be via an HTML parser, like HtmlAgilityPack:
string stringThatKeepsYourHtml = "<div id=....";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(stringThatKeepsYourHtml);
string whatUrLookingFor = doc.GetElementbyId("myId").InnerHtml;
这篇关于通过ID获取div内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文