Exemple #1
0
        public IList <HtmlData> ReadHtmlData(string url)
        {
            try
            {
                HttpWebRequest  request          = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response         = (HttpWebResponse)request.GetResponse();
                StreamReader    sr               = new StreamReader(response.GetResponseStream());
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.Load(sr);
                var nodess = doc.DocumentNode.DescendantsAndSelf("div").Where(tag => tag.Id.Contains("questionid"));

                foreach (var node in nodess)
                {
                    var htmlData = new HtmlData
                    {
                        Question = node.FirstChild.InnerText,
                        Option1  = node.FirstChild.NextSibling.NextSibling.InnerText,
                        Option2  =
                            node.FirstChild.NextSibling.NextSibling.NextSibling.InnerText,
                        Option3 =
                            node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling
                            .InnerText,
                        Option4 =
                            node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling
                            .NextSibling.InnerText,
                        CorrectAnswer =
                            node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling
                            .NextSibling.NextSibling.Attributes["value"].Value
                    };



                    this._htmlDataList.Add(htmlData);
                }
                sr.Close();

                return(this._htmlDataList);
            }
            catch (Exception ex)
            {
                throw new Exception("Error in reading htmlData", ex);
            }
        }
Exemple #2
0
        void a(IEnumerable <HtmlNode> nodes)
        {
            foreach (var node in nodes)
            {
                var htmlData = new HtmlData
                {
                    Question = node.FirstChild.InnerText,
                    Option1  = node.FirstChild.NextSibling.NextSibling.InnerText,
                    Option2  = node.FirstChild.NextSibling.NextSibling.NextSibling.InnerText,
                    Option3  =
                        node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling
                        .InnerText,
                    Option4 =
                        node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling
                        .NextSibling.InnerText,
                    CorrectAnswer =
                        node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling
                        .NextSibling.NextSibling.Attributes["value"].Value
                };

                // this._htmlDatas.Add(htmlData);
            }
        }