本帖最后由 zhuzhu837_1 于 2012-08-13 17:45:37 编辑

解决方案 »

  1.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;namespace SkinDemo
    {
        public partial class Form7 : Form
        {
            string webContent = "<html>"
    + "<ul>"
    + "<li calss='a1'><a><span>a1</span></a></li>"
    + "<li calss='a2'><a><span>a2</span></a></li>"
    + "<li calss='a3'><a><span>a3</span></a></li>"
    + "<li calss='a4'><a><span>a4</span></a></li>"
    + "<li calss='a5'><a><span>a5</span></a></li>"
    + "</ul>"
    + "</html>";        public Form7()
            {
                InitializeComponent();            webBrowser1.DocumentText = webContent;
            }        private void button1_Click(object sender, EventArgs e)
            {
                string content = webBrowser1.DocumentText;
                string reg = "<li calss='a3'><a><span>(.+?)</span></a></li>";
                Regex regex = new Regex(reg);
                Match match = regex.Match(content);
                MessageBox.Show(match.Groups[1].Value);
            }
        }
    }
      

  2.   


    楼上的是获取了字符串,那怎么把获取的字符串转为HtmlElement呢?
      

  3.   

     WebBrowser web = (WebBrowser)sender;                     HTMLDocument doc1 = (HTMLDocument)web.Document.DomDocument;
                IHTMLElementCollection ElementCollection = doc1.getElementsByTagName("Table");
                foreach (IHTMLElement item in ElementCollection)
                {
                    string strText = item.innerText.ToString();
    }这个是之前我做的一个,跟你要求应该差不多,你自己按照这个修改一下应该就可以了
      

  4.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;namespace SkinDemo
    {
        public partial class Form7 : Form
        {
            string webContent = "<html>"
    + "<ul>"
    + "<li calss='a1' id='a1'><a><span>a1</span></a></li>"
    + "<li calss='a2'  id='a2'><a><span>a2</span></a></li>"
    + "<li calss='a3' id='a3'><a><span>a3</span></a></li>"
    + "<li calss='a4' id='a4'><a><span>a4</span></a></li>"
    + "<li calss='a5' id='a5'><a><span>a5</span></a></li>"
    + "</ul>"
    + "</html>";        public Form7()
            {
                InitializeComponent();            webBrowser1.DocumentText = webContent;
            }        private void button1_Click(object sender, EventArgs e)
            {
                HtmlElementCollection colleciton = webBrowser1.Document.GetElementsByTagName("ul");
                foreach (HtmlElement ele in colleciton[0].Children)
                {
                    if (ele.GetAttribute("id") == "a3")
                    {
                        string v = ele.Children[0].Children[0].InnerText;//A标签中span的值
                        HtmlElement e = ele.Children[0];//就是你所需要的A标签
                    }
                }        }
        }
    }将li标签上面添加一个id
      

  5.   

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
      HtmlElementCollection li = webBrowser1.Document.GetElementsByTagName("li");
      for (int i = 0; i < li.Count;i++ )
      {
        if (li[i].GetAttribute("calss") == "a3")
        {
          HtmlElement link = li[i].FirstChild;
          MessageBox.Show(link.InnerHtml);
        }
        }
    }