我希望可以把网页中所有的按钮和有id的DIV都取出来以下是我以前写的代码里面的问题是我把网页转换为xml格式来分析,但是一个是doc.LoadXml(html);会很慢,另外一个是html一定要很规则的才可以,稍微不规则一点的html都不能解析成xml文件。请问除了转换成xml之外还有什么方法可以实现以上要求阿?谢谢!
 WebRequest request = WebRequest.Create(FormUrl);
                WebResponse response = request.GetResponse();
                Stream resStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(resStream, System.Text.Encoding.UTF8);
                
                string html = sr.ReadToEnd();
                resStream.Close();
                sr.Close();
                resStream.Dispose();
                response.Close();
                html = html.Replace("\r", "").Replace("\n", "").Replace(@"\", "");
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(html);
                M_list.Clear();
                ParseHtml(doc.DocumentElement);       public void ParseHtml(XmlNode node)
        {
            WFFormParse fp = null;
            IEnumerator ienum = node.GetEnumerator();
            while (ienum.MoveNext())
            {
                fp = new WFFormParse();
                XmlNode Currentnode = (XmlNode)ienum.Current;                if (Currentnode.ChildNodes.Count > 0)
                {
                    ParseHtml(Currentnode);//recursion
                }
                if (Currentnode.Attributes != null)
                {
                    if (Currentnode.Name == "input" && Currentnode.Attributes != null && Currentnode.Attributes["id"] != null && Currentnode.Attributes["type"].InnerText == "submit")
                    {
                        if (Currentnode.Attributes["value"] != null)
                        {
                            fp.Type = ButtonType.Button;
                            fp.ID = Currentnode.Attributes["id"].InnerText;
                            fp.DisplayName = Currentnode.Attributes["value"].InnerText;                        }
                        else
                        {
                            fp.Type = ButtonType.Button;
                            fp.ID = Currentnode.Attributes["id"].InnerText;
                            fp.DisplayName = Currentnode.Attributes["id"].InnerText;
                        }                    }
                    else if (Currentnode.Name == "div" && Currentnode.Attributes != null && Currentnode.Attributes["id"] != null)
                    {
                        fp.Type = ButtonType.Div;
                        fp.ID = Currentnode.Attributes["id"].InnerText;
                        fp.DisplayName = Currentnode.Attributes["id"].InnerText;
                    }
                    if (fp.ID != null)
                    {
                        M_list.Add(fp);
                    }
                }            }

解决方案 »

  1.   

    用webrequest ,webresponse ,不如直接用webbrowser(也就是ie) ,再获得document
    分析。
      

  2.   

    up  
    我现在想要得主要还是如何去分析Html资料获得我要得Button和有id的div呢
      

  3.   

    IDocumentX(x指1,2,3,4分别代表ie的版本)也可以获得html,另外xmldocument要求比较严格,很多情况下不能使用。建议还是用webbrowser
      

  4.   

    哦?webbrowser要怎么用啊?能给一些简单的代码介绍么?比如就把有 id 的 div取出来谢谢如果成功我再加分
      

  5.   

    楼主参看http://www.pcvc.net/category/content.asp?sendid=178http://www.codefans.com/ArticleView/Article_6775.html
      

  6.   

    或者http://blogger.org.cn/blog/more.asp?name=wonderow&id=503
      

  7.   

    正则表达式要怎么写呢?
    还有就是我还要获取这个div的 id之类的信息
    这个要怎么获得呢?