0

Is there an easy way to count the nodes in a HTML file? I also need to count nodes of a certain type such as div etc.

I'd like to do this if possible without having to use an external library like HTMLAgilityPack if possible. Also, the HTML I'm dealing with is not guarenteed to be well formed and valid.

Is there a way to do this from C#?

Thanks.

Jimmy Collins
  • 3,294
  • 5
  • 39
  • 57

3 Answers3

0

If you have XHTML you can load it in a XDocument and use XML manipulation API or LINQ to XML to count the particular modes.
If you don't you can try using Regular Expressions. But this one works in small number of interesting tags since you have to define manually an expression for each tag.

ViktorZ
  • 901
  • 1
  • 10
  • 26
0

With LinqToXml API, you can easily parse and loop through all the nodes of an HTML document. You can find helpful articles related to LinqToXml but all in context of parsing XML documents.

Following is a similar thread from StackOverflow : C# Is there a LINQ to HTML, or some other good .Net HTML manipulation API?

Community
  • 1
  • 1
Pawan Mishra
  • 7,212
  • 5
  • 29
  • 39
0

first of all. are your sure a client-side solution using javascript isn't sufficent to your needs? because the easiest way to count nodes within an HTML document is using jQuery on the client-side browser.

<script src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script>
    $('html').children() // will give you all child elements of the html element
    $('body').children() // same for body element
    $('body').children('div') // will give you just the direct children elements of 'div' type
    $('body').find('div') // will give you all the nested elements of 'div' type
</script>

if you are unfamilier with jQuery then take a look at www.jquery.com

if u still need a C# solution for server-side parsing of the document then then i would recommend to use HTMLAgilityPack (even thou you wish not to). writing your own parser seems to me like a waste of time as you need to consider malformed html/xml and such which can be a pain.

try and use this s-overflow article: What is the best way to parse html in C#?

hope it will satisfy your needs

Community
  • 1
  • 1
marxus
  • 462
  • 2
  • 6