So we have an XML file with a very simple implementation of XLink:
<root xmlns:xlink="http://www.w3.org/1999/xlink">
<firstChild id="ID1" />
...
<ref xlink:href="#ID1" />
</root>
Let's assume the XLink implementation won't get any more complicated than that. However the important point is that the element referred to (in this case firstChild) could appear anywhere in the document, anywhere in the hierarchy.
In an XPath lookup you could find the element referred to by the node by using an expression like:
//*[@id='ID1']
What's the best equivalent using Linq to XML? I'd have thought something along these lines:
XDocument doc = XDocument.Load("file.xml");
var dest = xDoc.Descendants().Where(e => (string)e.Attribute("id") == "ID1").SingleOrDefault();
Not actually tested yet. But in general terms, if the XML document is quite large, is the LINQ way going to be inefficient (since it's using an enumeration of all descendants on the XDocument)? Would it be better to revert to an XPathNavigator and just use the XPath expression?
If this kind of thing is okay to do in LINQ, is there a better method than what I wrote? LINQ is still only a few days old to me... it's awesome, but I wonder if it has efficiency limitations for certain operations.
Thanks!