1

I'm usign HTML Agility Pack in C# and I have two HtmlNodeCollection. Can I join both or is there other ways to get both in one HtmlNodeCollection?

One:

HtmlNodeCollection butiks = doc.DocumentNode.SelectNodes("//div[contains(@class,'butik-large-image')]");

Two:

HtmlNodeCollection butiks = doc.DocumentNode.SelectNodes("//div[contains(@class,'butik small left')]");
CodeCaster
  • 147,647
  • 23
  • 218
  • 272

1 Answers1

2

HtmlNodeCollection inherits IList<HtmlNode> which inherits IEnumerable<HtmlNode> on which you can call the Enumerable.Concat() extension method to create a new enumerable containing both sources. See How to concatenate two IEnumerable<T> into a new IEnumerable<T>?.

You can also just select both sets of nodes by using an "or" in your Xpath expression:

//div[contains(@class,'butik-large-image') or contains(@class,'butik small left')]

Please note this contains() expects given classes in the given order. If you don't want that, use parentheses and and:

//div[contains(@class,'butik-large-image') 
or 
( 
    contains(@class,'left') and
    contains(@class,'small') and
    contains(@class,'butik')
)]

See How can I select an element with multiple classes with Xpath? for a proper implementation of that and, as above code will match false positives because it doesn't check for the classes as separate words.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • if my class attribute have to class link this `class='butik large'` can i use this way? –  Sep 12 '15 at 17:08
  • @AHIR, see the or- and and-conditions, just add the ones you need. It's a simple boolean expression, it can grow as wide or narrow as you please. – Abel Sep 12 '15 at 20:36