2

Is it possible to use XPath with .NET, without using any external libraries? Is it natively supported, and can it parse "invalid HTML" (such as tags not being closed etc)?

I would really hate to have to use regular expressions for this, as clearly stated here: RegEx match open tags except XHTML self-contained tags

I've also had bad experiences with regular expressions when it comes to HTML parsing.

Community
  • 1
  • 1
Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187

3 Answers3

8

Yes, XPath is natively supported. No, it will not parse tag soup. You'll probably want to use the HTML Agility Pack for that instead.

SpruceMoose
  • 9,737
  • 4
  • 39
  • 53
Jeff Hubbard
  • 9,822
  • 3
  • 30
  • 28
4

XPath has been supported in .NET since day 1. However, it only supports well-formed XML. Not all valid HTML is well-formed XML, and unclosed tags are not well-formed XML.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
2

Yes. See System.Xml.XPath.XPathExpression. It lives in System.Xml.dll, which is included on any machine that has the .NET Framework installed.

Not sure about the unclosed HTML tags question. A small experiment should answer that pretty quickly.

dthorpe
  • 35,318
  • 5
  • 75
  • 119