Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement a simple XPath lookup
    primarykey
    data
    text
    <p>We have an XML file with a very simple implementation of <a href="https://en.wikipedia.org/wiki/XLink" rel="nofollow noreferrer">XLink</a>:</p> <pre><code>&lt;root xmlns:xlink="http://www.w3.org/1999/xlink"&gt; &lt;firstChild id="ID1" /&gt; ... &lt;ref xlink:href="#ID1" /&gt; &lt;/root&gt; </code></pre> <p>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.</p> <p>In an XPath lookup you could find the element referred to by the &lt;ref> node by using an expression like:</p> <pre><code>//*[@id='ID1'] </code></pre> <p>What's the best equivalent using <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query#LINQ_to_XML_.28formerly_called_XLINQ.29" rel="nofollow noreferrer">LINQ&nbsp;to&nbsp;XML</a>? I'd have thought something along these lines:</p> <pre><code>XDocument doc = XDocument.Load("file.xml"); var dest = xDoc.Descendants().Where(e =&gt; (string)e.Attribute("id") == "ID1").SingleOrDefault(); </code></pre> <p>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?</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload