Pregunta

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

¿Fue útil?

Solución

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();
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top