简体   繁体   中英

How to get element from HTML

Hello i trying to insert the ddl that in html to System.Collections.ArrayList

I making HttpWebRequest and HttpWebResponse

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

Now i have all Html page in MyHtmlPage(streamRead),and inside i have the DropDownList with all data that i need to store in the System.Collections.ArrayList (or standart array if it posible)

this is the pice of all html page and here is my ddl id- ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts that i need to store

<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> 

sow any ideas how can i pul this data from MyHtmlPage to array?(server side)

You can use Html Agility Pack to parse html documents. You might want to look into that.

I find another way i get retList with all data.

    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);
             }
                   }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM