Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 283801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:24:15+00:00 2026-05-12T05:24:15+00:00

We have an XML file with a very simple implementation of XLink : <root

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:24:16+00:00Added an answer on May 12, 2026 at 5:24 am

    XPathNavigator isn’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:

    XDocument doc = ...;
    var id2elem = (from e in doc.Descendants()
                   let id = e.Attribute("id")
                   where id != null
                   select new { Id = id.Value, Element = e })
                  .ToDictionary(kv => kv.Id, kv => kv.Element);
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 206k
  • Answers 206k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The error is quite clear - you're using HTTP, you… May 12, 2026 at 9:09 pm
  • Editorial Team
    Editorial Team added an answer (Components[i] as ComponentClass).Visible May 12, 2026 at 9:09 pm
  • Editorial Team
    Editorial Team added an answer I generally add 'DTO' to the end of the Class… May 12, 2026 at 9:09 pm

Related Questions

I have a simple task. I have an existing project with a web service
I am trying something very simple, but for some reason it does not work.
We recently started providing a data extract to clients via an XML. We have
Is there anything similar to getElementById in actionscript? I'm trying to make a prototype

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.