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

  • SEARCH
  • Home
  • 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

Related Questions

I have very simple persistance.xml file: <?xml version=1.0 encoding=UTF-8?> <persistence version=1.0 xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/persistence
I have a map that reads an XML file; it's all very simple and
I have the following very simple XML file and I want to quickly parse
i have this very simple download page to get an xml file. the script
I'm new to Linq to Xml. I have a very simple xml file like
I have a very simple XML file, that looks like this: <Data><Day Num=4197> <Date>270611</Date>
I have a very simple XML file: <?xml version=1.0 encoding=ISO-8859-1?> <Data><Day Num=4197> <Date>270611</Date> <Energy>47</Energy>
I have a very simple xml file that I would like to create a
I have an XML file that is very long, but here is a shot
I have a very large XML file which has like 40000 data, and when

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.