Parsing Radio button name only bringing up one value

和自甴很熟 提交于 2019-12-13 03:02:11

问题


I have written some code to parse the name from some radio buttons.

<div id="First" class="Size-Inputs"> 
<input rel="8" type="radio" value="13051374" name="idProduct-13051359"/> <span rel="L">L</span>
<input rel="8" type="radio" value="13051373" name="idProduct-13051359"/> <span rel="M">M</span>
<input rel="8" type="radio" value="13051372" name="idProduct-13051359"/> <span rel="S">S</span>
<input rel="8" type="radio" value="13051375"name="idProduct-13051359"/> <span rel="XL">XL</span> </div>

The problem which I am having when I try to parse the span rel to get the size names eg L,M,S,XL it is only coming up with the value L four times.

The code I am using is;

HtmlNodeCollection link = doc.DocumentNode.SelectNodes("//*[@id='First']/input");
if (link != null)
{
    foreach (HtmlNode item in link)
    {
        string name = item.SelectSingleNode("//*[@id='First']/span").InnerText;
        Console.WriteLine(name);
    }
    Console.ReadLine();
}

I was just wondering why the it is only picking up one value and printing it four times and how can you make it pick up the span rel for each of the variant input. Thanks for any help which you can provide


回答1:


It seems that you don't need <input> element but <span> element. And <span> is not child of <input>. So assuming that it isn't typo or paste error, you can select <span> directly without selecting <input> element first :

HtmlNodeCollection link = doc.DocumentNode.SelectNodes("//*[@id='First']/span");
if (link != null)
{
    foreach (HtmlNode item in link)
    {
        string name = item.InnerText;
        Console.WriteLine(name);
    }
    Console.ReadLine();
}


来源:https://stackoverflow.com/questions/21962035/parsing-radio-button-name-only-bringing-up-one-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!