2011-04-24 242 views
0

我使用HtmlAgilityPack HtmlNode,我想获得HtmlAgilityPack HtmlNode如何获得<select>和<option>标签

<select> and <option> and all other html tags. 

目前HtmlNode解析HTML只,表,格...标签。我如何获得选项并选择标签。

我当前的代码:

if (htmlContent != null) 
{ 
    doc.LoadHtml(htmlContent); 

    if (doc.DocumentNode.SelectNodes("//*") == null) 
    { 
     List<TagValuePair> tempList = new List<TagValuePair>(); 
     tempList.Add(new TagValuePair("Error!", htmlContent, -1)); 
     return tempList; 
    } 

    foreach (HtmlNode tag in doc.DocumentNode.SelectNodes("//*")) 
    { 
     try 
     { 
      if (!string.IsNullOrEmpty(tag.InnerHtml)) 
      { 
       if (!tagAppearance.Keys.Contains(tag.Name)) 
       { 
        tagAppearance.Add(tag.Name, 1); 
       } 
       else 
        tagAppearance[tag.Name] = tagAppearance[tag.Name] + 1; 

       tagsValues.Add(
        new TagValuePair(tag.Name, tag.InnerHtml.Trim(), 
        tagAppearance[tag.Name]) 
       ); 
      } 
     } 
     catch 
     { 
      return null; 
     } 
    } 
} 

我尝试用

doc.DocumentNode.SelectNodes("//option"); 

和我得到的选项标签,但没有其他人。如何让所有+选项,并选择

编辑:

需要从这个网站从选项得到的所有数据:

<select onchange="javascript:submitGlobalDiscountSvetisce(this.value);submitGlobalDiscount(this.value);" name="datumper"> 
        <option value="18.6.2011|7">18.jun. 7 noči od 515,00&nbsp;EUR</option> 
<option value="25.6.2011|7">25.jun. 7 noči od 515,00&nbsp;EUR</option> 
<option value="2.7.2011|7">2.jul. 7 noči od 515,00&nbsp;EUR</option> 
<option value="9.7.2011|7">9.jul. 7 noči od 515,00&nbsp;EUR</option> 
<option value="16.7.2011|7">16.jul. 7 noči od 515,00&nbsp;EUR</option> 
<option value="23.7.2011|7">23.jul. 7 noči od 515,00&nbsp;EUR</option> 
<option value="30.7.2011|7">30.jul. 7 noči od 529,00&nbsp;EUR</option> 
<option value="6.8.2011|7">6.avg. 7 noči od 529,00&nbsp;EUR</option> 
<option value="13.8.2011|7">13.avg. 7 noči od 529,00&nbsp;EUR</option> 
<option value="20.8.2011|7">20.avg. 7 noči od 529,00&nbsp;EUR</option> 
<option value="3.9.2011|7">3.sep. 7 noči od 487,00&nbsp;EUR</option> 
<option value="10.9.2011|7">10.sep. 7 noči od 487,00&nbsp;EUR</option> 
<option value="17.9.2011|7">17.sep. 7 noči od 487,00&nbsp;EUR</option> 
<option value="24.9.2011|7">24.sep. 7 noči od 487,00&nbsp;EUR</option> 
<option value="1.10.2011|7">1.okt. 7 noči od 529,00&nbsp;EUR</option> 
<option value="8.10.2011|7">8.okt. 7 noči od 529,00&nbsp;EUR</option> 
<option value="15.10.2011|7">15.okt. 7 noči od 529,00&nbsp;EUR</option> 
<option value="22.10.2011|7">22.okt. 7 noči od 567,00&nbsp;EUR</option> 
<option value="29.10.2011|7">29.okt. 7 noči od 567,00&nbsp;EUR</option> 

       </select> 

回答

3

默认的选项标签是专门处理(历史原因)。见我在此线程答案在这里SO:Parsing HTML Reading Option Tag Content with HtmlAgillityPack

+0

thx for answer。所以我必须手动解析选项和所有其他标签并通过联合加入。我对吗? – senzacionale 2011-04-24 14:42:43

+0

@senzacionale - 不,你需要使用ElementFlags;从那里删除“选项”标签。看到另一个线程http://stackoverflow.com/questions/4755486/xhtml-parsing-with-htmlagilitypack – 2011-04-24 14:44:36

+0

如果我使用你的代码:foreach(HtmlNode节点在docs.DocumentNode.SelectNodes(“//选择[@ id ='onoffaci '] // option“)) 我得到的对象引用未设置为对象的实例。 – senzacionale 2011-04-24 14:53:06

0

可以使用html.DocumentNode.SelectSingleNode( “//选择[@ ID = 'idElement']”)OuterHtml此行带来的选择和选项。

Happy Codes