developer tip

HtmlAgilityPack : 전체 HTML 문서를 문자열로 가져 오기

optionbox 2020. 11. 8. 09:48
반응형

HtmlAgilityPack : 전체 HTML 문서를 문자열로 가져 오기


HtmlAgilityPack 에 HtmlDocument 개체에서 전체 HTML 태그를 문자열 로 반환하는 기능이 있습니까?


물론 다음과 같이 할 수 있습니다.

HtmlDocument doc = new HtmlDocument();
// call one of the doc.LoadXXX() functions
Console.WriteLine(doc.DocumentNode.OuterHtml);

OuterHtml은 전체 html을 포함합니다.


Url 및 Get webResponse를 전달하는 WebRequest를 만들 수 있습니다. WebResponse에서 ResponseStream을 가져 와서 문자열로 읽습니다.

string result = string.Empty;

WebRequest req = WebRequest.Create(Url);
WebResponse res= wrq.GetResponse();    
StreamReader reader = new StreamReader(res.GetResponseStream());
result = reader.ReadToEnd();    
reader.Close();
res.Close();

도움이 되었기를 바랍니다.

참고 URL : https://stackoverflow.com/questions/5183385/htmlagilitypack-get-whole-html-document-as-string

반응형