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
  • 14,077
  • 22
  • 91
  • 177

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
  • 8,885
  • 4
  • 37
  • 52
Jeff Hubbard
  • 9,714
  • 3
  • 29
  • 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
  • 159,224
  • 26
  • 237
  • 393
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
  • 34,664
  • 5
  • 74
  • 119