In our production code, I’ve seen XML attributes being read using explicit XName.Get call:
var name = element.Attribute (XName.Get ("name"));
I used to always pass a string to Attribute:
var name = element.Attribute ("name");
This is more readable but I wonder if there is any difference in logic or performance.
Well, there are two parts to this:
Are they calling the same
Attributemethod?Yes. There’s only one
XElement.Attributemethod, with anXNameparameter, which means that in the latter case you are using the implicit string toXNameconversion.Does the implicit string to
XNameconversion do the same asXName.Get?This isn’t guaranteed – the documentation doesn’t mention it. But I have no reason to doubt SLaks’ analysis that the current implementation is the same.
Personally I always either use the conversion from string to
XNameor the addition operator betweenXNamespaceand string to get anXName. I can’t remember the last time I referred to it explicitly.The conversions available are one of the beautiful things about LINQ to XML – it seems pointless to ignore them, IMO.