如何从HTML获取元素

时间:2014-04-09 06:30:18

标签: c# asp.net dom

您好我试图将html中的ddl插入System.Collections.ArrayList

我制作了HttpWebRequest和HttpWebResponse

HttpWebResponse RedirectResponse = RedirectToUrl("https://servicestest.com/Pages/Trans.aspx");
Stream streamResponse = RedirectResponse.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
MyHtmlPage= streamRead.ReadToEnd();

现在我在MyHtmlPage(streamRead)中拥有所有Html页面,并且在内部我有DropDownList,其中包含我需要存储在System.Collections.ArrayList中的所有数据(如果它可行,则为standart数组)

这是所有html页面的pice,这是我需要存储的ddl id - ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts

<div id="ctl00_PlaceHolderMain_AccountsDDL_ddlAccountsWarrper" class="positionRelative "> 
<select name="ctl00$PlaceHolderMain$AccountsDDL$ddlAccounts"  id="ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts" class="margin5 positionAbsolute"> 
        <option selected="selected" value="1" title=" some thhh ">Some info </option> 
        <option value="2" title=" fff"> some info </option> 

        </select> 

        </div> 

播下任何想法如何将这些数据从MyHtmlPage传送到数组?(服务器端)

2 个答案:

答案 0 :(得分:3)

您可以使用Html Agility Pack来解析html文档。你可能想看一下。

答案 1 :(得分:0)

我找到另一种获取所有数据的retList的方法。

    System.Collections.ArrayList retList = new 
    System.Collections.ArrayList();
    mshtml.HTMLDocument doc = new mshtml.HTMLDocument();
    mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)doc;
    doc2.clear();
    doc2.write(MyHtmlPage);
    mshtml.IHTMLDocument3 doc3 = (mshtml.IHTMLDocument3)doc2;
    mshtml.HTMLSelectElement selectToGet = (mshtml.HTMLSelectElement)doc3.getElementById(IDToGet);
                    if (selectToGet != null)
                    {
foreach (mshtml.HTMLOptionElement optToGet in selectToGet.getElementsByTagName("option"))
                        {
if (optToGet.value != null && optToGet.innerText != null)
     {
         retList.Add(optToGet.value + "\t" + optToGet.innerText);
     }
          else if (optToGet.innerText != null)
            {
               retList.Add("value not found" + "\t" + optToGet.innerText);
             }
                   }
      }