示例#1
0
        /// <summary>
        /// Called to extract a list from the specified URL.
        /// </summary>
        /// <param name=”url”>The URL to extract the list
        /// from.</param>
        /// <param name=”listType”>What type of list, specify
        /// its beginning tag (i.e. <UL>)</param>
        /// <param name=”optionList”>Which list to search,
        /// zero for first.</param>

        public void ProcessTable(Uri url, int tableNum)
        {

            //ignore bad cert code
            IgnoreBadCertificates();

            //code to allow program with work with different authentication schemes
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            List<String> list = new List<String>();
            //WebRequest http = HttpWebRequest.Create(url);
            FileWebRequest http = (FileWebRequest)WebRequest.Create(url);
            //HttpWebResponse response = (HttpWebResponse)http.GetResponse();
            FileWebResponse response = (FileWebResponse)http.GetResponse();
            Stream istream = response.GetResponseStream();
            ParseHTML parse = new ParseHTML(istream);
            StringBuilder buffer = new StringBuilder();
            bool capture = false;
            Advance(parse, "table", tableNum);
            int ch;
            while ((ch = parse.Read()) != -1)
            {
                if (ch == 0)
                {
                    HTMLTag tag = parse.Tag;
                    if (String.Compare(tag.Name, "tr", true) == 0)
                    {
                        list.Clear();
                        capture = false;
                        buffer.Length = 0;
                    }
                    else if (String.Compare(tag.Name, "/tr", true) == 0)
                    {
                        if (list.Count > 0)
                        {
                            ProcessTableRow(list);
                            list.Clear();
                        }
                    }
                    else if (String.Compare(tag.Name, "td", true) == 0)
                    {
                        if (buffer.Length > 0)
                            list.Add(buffer.ToString());
                        buffer.Length = 0;
                        capture = true;
                    }
                    else if (String.Compare(tag.Name, "/td", true) == 0)
                    {
                        list.Add(buffer.ToString());
                        buffer.Length = 0;
                        capture = false;
                    }
                    else if (String.Compare(tag.Name, "/table", true) == 0)
                    {
                        break;
                    }
                }
                else
                {
                    if (capture)
                        buffer.Append((char)ch);
                }
            }
        }//end ProcessTable