Is it just me? I find LINQ to XML to be sort of cumbersome, compared to XPath.

Posted by Cheeso on Stack Overflow See other posts from Stack Overflow or by Cheeso
Published on 2009-09-18T04:23:11Z Indexed on 2010/04/25 4:33 UTC
Read the original article Hit count: 338

Filed under:
|
|
|

I am a C# programmer, so I don't get to take advantage of the cool XML syntax in VB.

Dim itemList1 = From item In rss.<rss>.<channel>.<item> _
                Where item.<description>.Value.Contains("LINQ") Or _
                      item.<title>.Value.Contains("LINQ")

Using C#, I find XPath to be easier to think about, easier to code, easier to understand, than performing a multi-nested select using LINQ to XML. Look at this syntax, it looks like Greek swearing:

var waypoints = from waypoint in gpxDoc.Descendants(gpx + "wpt") 
          select new 
          { 
            Latitude = waypoint.Attribute("lat").Value, 
            Longitude = waypoint.Attribute("lon").Value, 
            Elevation = waypoint.Element(gpx + "ele") != null ? 
                waypoint.Element(gpx + "ele").Value : null, 
            Name = waypoint.Element(gpx + "name") != null ? 
                waypoint.Element(gpx + "name").Value : null, 
            Dt = waypoint.Element(gpx + "cmt") != null ? 
                waypoint.Element(gpx + "cmt").Value : null 
          }; 

All the casting, the heavy syntax, the possibility for NullPointerExceptions. None of this happens with XPath.

I like LINQ in general, and I use it on object collections and databases, but my first go-round with querying XML led me right back to XPath.

Is it just me?

Am I missing something?


EDIT: someone voted to close this as "not a real question". But it is a real question, stated clearly. The question is: Am I misunderstanding something with LINQ to XML?

© Stack Overflow or respective owner

Related posts about xpath

Related posts about LINQ