Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is better XPath or Linq2Xml

What is the "best" way to search in xml?
Xpath or Linq2xml.

I'm asking this because we need to do a lot of searching in xml.
I'm always using XPath (since I've been using this from .NET 1.1). But with the introduction of Linq you can easely use Linq2Sql.

Regards, M.

like image 296
user29964 Avatar asked Jan 28 '10 08:01

user29964


1 Answers

i use both extensively also xsl

they have very different uses imo

xpath is great for manipulating xml documents wheras linq2xml is great for mapping them into object collections.

In other words i regularly have applications that involve both.

for instance parsing csv into a given xml structure is almost cherry picked for xslt and xpath wheras linq2XMl will give you problems if you have an xml document that has optional elements. so i tend to use xpath to really lock down the xml format so that it is explicit and to keep my linq2xml mapping very very simple.

The result is a lot less bugs and much faster development.

no idea why the guy is talking about linq2xsd ... its a discontinued project that has very very little documentation. stay away from it.

Xdocument is an object that is actually enjoyable to work with ... xmldocument is one that is just fiddly imo. Obviously it depends on the task at hand, but the lack of xpath 2.0 makes me tend to use it as a data cleanser and then let linq2XMl do the real work.

as far as search goes, you can do everything that linq2xml does in xpath, the thing is that syntactically i far prefer to use linq2sql and play with strongly typed collections than mess about with xpath. Its much easier to come back to at a later date and adapt. Also you dont have to worry about syntax differences between xpath implementations and especially with regex implementations

like image 86
John Nicholas Avatar answered Sep 19 '22 13:09

John Nicholas