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 <ref> 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();
I have not actually tested it 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.
XPathNavigatorisn’t going to be any more efficient here, because it will still have to enumerate all descendants to find them – there’s no magic dust there. If you want it to be more efficient than that, you’ll need an index, and no built-in XML API provides them out of the box, so you’ll have to roll out your own. For example:and then use that dictionary to lookup nodes by ID whenever you need to. Obviously, this is only worthwhile if lookups are relatively frequent, and not if you just need to do it once or twice.