jason
jason

Reputation: 7164

Get list element when HTML Parsing

I have a piece of HTML like this :

<li class="myclass">
            <ul class="myclass2">
                <li><span>Name1</span><span>Value1</span></li><li><span>Name2</span><span>Value2</span></li><li><span>Name3</span><span>Value3</span></li>
            </ul>
        </li>

I'm trying to parse this HTML like this using HTML Agility Pack :

var values = mydetails.DocumentNode.QuerySelector(".myclass").QuerySelector("ul").InnerHtml;

This gives me this part :

<li><span>Name1</span><span>Value1</span></li><li><span>Name2</span><span>Value2</span></li><li><span>Name3</span><span>Value3</span></li>

But I don't know how to go further, I need Names and Values but I don't know how to get them. Can you show me a way to do it? Thanks.

Upvotes: 0

Views: 924

Answers (2)

Mihir Kale
Mihir Kale

Reputation: 1118

You can use a Regular expression to get values from all the tags

public void ProcessSpans(string inputHTML)
{
    string pattern = @"<span([^>]*)class=\""(\w+)\""([^>]*)>(.*)<\/span>";
    RegexOptions regexOptions = RegexOptions.Multiline;
    Regex regex = new Regex(pattern, regexOptions);
    var matches = regex.Matches(inputHTML);
    //Process the matches with your logic. 
}

Then call the method as such

var values = mydetails.DocumentNode.QuerySelector(".myclass").QuerySelector("ul").InnerHtml;
ProcessSpans(values);

Upvotes: 1

jenald
jenald

Reputation: 182

You should give your items an html-id, then it should be very easy to get this element in javascript. I don't know your framework, but in others this is very easy, for example in jQuery: $("#id")

Upvotes: 1

Related Questions