Saturday, February 18, 2017

Parsing HTML with AgilityPack using XPath


http://zvon.org/xxl/XPathTutorial/Output/examples.html


//GGG/descendant::*

     <AAA>
          <BBB>
               <CCC/>
               <ZZZ/>
          </BBB>
          <XXX>
               <DDD>
                    <EEE/>
                    <FFF>
                         <HHH/>
                         <GGG>
                              <JJJ>
                                   <QQQ/>
                              </JJJ>
                              <JJJ/>
                         </GGG>
                         <HHH/>
                    </FFF>
               </DDD>
          </XXX>
          <CCC>
               <DDD/>
          </CCC>
     </AAA>


protected List<HtmlNode> GetHtmlNodes(CodeSampleModel model)
        {
            var html = new System.Net.WebClient().DownloadString(model.Url);
            List<HtmlTextNode> textNodes = new List<HtmlTextNode>();

            string testString = html;

            HtmlDocument doc = new HtmlDocument();
            //doc.LoadHtml("<!DOCTYPE html><html><head><title>Profile</title><meta charset = 'utf-8'/></head><body><div>jsdfjslf lskfjlkdjf profile\n<p>xxx Profile<span class='profile'>profile</span></p></div></body></html>");

            if (doc.ParseErrors.Any())
            {
                model.Message = "Parse errors were encountered.";
                return null;
            }
            else
            {
                List<HtmlNode> allNodes = doc.DocumentNode.SelectNodes("//html//descendant::*").ToList();
                return allNodes;
            }

        }

No comments:

Post a Comment