views:

414

answers:

4

Hi,

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.

A: 

Either. It depends.

Depending on your (and your team's) knowledge (i.e. XPath will not be effective is no one knows XPath, but all know LINQ to XML). Also some operations can be easier in one or the other.

Richard
A: 

You will need to define criteria first to judge what is best. And you need to decide whether you want to compare XPath 1.0 or 2.0 with LINQ to XML. Microsoft does not support XPath 2.0 but third party solutions exist, like Saxon 9 or like XQSharp.

Martin Honnen
+3  A: 

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

John Nicholas
this is a great explanation, thx mate
Sem Dendoncker
np glad to help
John Nicholas