MSDN.WhiteKnight - Stack Overflow answers
Ответ на "Как распарсить HTML в .NET?"
Answer 738668
Если требования к производительности не очень высокие, можно использовать COM-объект Internet Explorer (добавить ссылку на Microsoft HTML Object Library):
public static List<string> ParseLinks(string html) { List<string> res = new List<string>(); mshtml.HTMLDocument doc = null; mshtml.IHTMLDocument2 d2 = null; mshtml.IHTMLDocument3 d = null; try { doc = new mshtml.HTMLDocument();//инициализация IE d2 = (mshtml.IHTMLDocument2)doc; d2.designMode = "On"; d2.write(html); d = (mshtml.IHTMLDocument3)doc; var coll = d.getElementsByTagName("a");//получить коллекцию элементов по имени тега object val; foreach (mshtml.IHTMLElement el in coll)//извлечь атрибут href из всех элементов { val=el.getAttribute("href"); if (val == null) continue; res.Add(val.ToString()); } } finally { //освобождение ресурсов if (doc != null) Marshal.ReleaseComObject(doc); if (d2 != null) Marshal.ReleaseComObject(d2); if (d != null) Marshal.ReleaseComObject(d); } return res; }
Content is retrieved from StackExchange API.
Auto-generated by ruso-archive tools.