c# Parse HTML with AngleSharp, find DIV with part of ID

4.1k views Asked by At

I just try the following for findng all div's in HTML with known Part of the ID:

var AllDiv = document.All.Where(m => m.LocalName == "div" && m.GetAttribute("id").StartsWith("new_order_"));

It allways results in an NullReferenceException. I also try it with "Contrains", but with the same result.

The div's look like:

<div id="new_order_50000_US">...</div>
<div id="new_order_4000_EU">...</div>

Can anybody help me with this?

THX

1

There are 1 answers

1
har07 On BEST ANSWER

You might need to check if current div has id attribute before checking the attribute value to avoid NRE :

var AllDiv = document.All.Where(m => m.LocalName == "div" && 
                                     m.HasAttribute("id") && 
                                     m.GetAttribute("id").StartsWith("new_order_"));