Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 754
Cheeso Avatar asked Sep 18 '09 04:09

Cheeso


People also ask

Can we use LINQ for XML?

LINQ to XML is an XML programming interface. LINQ to XML is a LINQ-enabled, in-memory XML programming interface that enables you to work with XML from within the . NET programming languages. LINQ to XML is like the Document Object Model (DOM) in that it brings the XML document into memory.

Which of the following options is created to retrieve data into XML using LINQ?

The LINQ to XML will bring the XML document into memory and allows us to write LINQ Queries on in-memory XML document to get the XML document elements and attributes. To use LINQ to XML functionality in our applications, we need to add "System. Xml. Linq" namespace reference.

Which of the following link class is used to query against XML?

IEnumerable<T> can be used to query the XML data and for this standard query the operators show up as extension methods on any object that implements IEnumerable<T> and can be invoked like any other method.


1 Answers

Yes, the example you've given is unweildy.

But with LINQ comes the flexibility to refactor away the unweildiness.

Here's an example of how I would improve it. (This is done without any testing at all, and I don't even know the real class names, but it should convey the idea)

static class LinqXmlExtension
{
    public static NodeThingy ElementOrNull(this XmlElement ele, string searchString)
    {
         return (ele.Element(searchString) != null ? ele.Element(searchString).Value : null);
    }
}

//
/////////////////////////////////////////////////////////////////

var waypoints = from waypoint in gpxDoc.Descendants(gpx + "wpt")           
                select new           
                {             
                      Latitude  = waypoint.Attribute("lat").Value,             
                      Longitude = waypoint.Attribute("lon").Value,
                      Elevation = waypoint.ElementOrNull(gpx + "ele"),
                      Name      = waypoint.ElementOrNull(gpx + "name"),
                      Dt        = waypoint.ElementOrNull(gpx + "cmt")           
                 };
like image 129
Andrew Shepherd Avatar answered Sep 28 '22 00:09

Andrew Shepherd