参见英文答案 >
What is the best way to parse html in C#? [closed]15个
有解决HTML代码的方法吗?
有解决HTML代码的方法吗?
即GetElementByTagName(“abc”).GetElementByTagName(“tag”)
我有这个代码块…
private void LoadProfilePage() { string sURL; sURL = "http://www.abcd1234.com/abcd1234"; WebRequest wrGETURL; wrGETURL = WebRequest.Create(sURL); //WebProxy myProxy = new WebProxy("myproxy",80); //myProxy.BypassProxyOnLocal = true; //wrGETURL.Proxy = WebProxy.GetDefaultProxy(); Stream objStream; objStream = wrGETURL.GetResponse().GetResponseStream(); if (objStream != null) { StreamReader objReader = new StreamReader(objStream); string sLine = objReader.ReadToEnd(); if (String.IsNullOrEmpty(sLine) == false) { .... } } }
解决方法
你可以使用优秀的
HTML Agility Pack.
This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don’t HAVE to understand XPATH nor XSLT to use it,don’t worry…). It is a .NET code library that allows you to parse “out of the web” HTML files. The parser is very tolerant with “real world” malformed HTML. The object model is very similar to what proposes System.Xml,but for HTML documents (or streams).