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 6342519
In Process

The Archive Base Latest Questions

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

How can I do a Linq query for this? I have two xml documents,

  • 0

How can I do a Linq query for this?

I have two xml documents, doc1.xml, and doc2.xml. How can I find for each “file” element in doc1 where doc2 has a “file” element with the exact same “path” attribute, but any “link” child element of this “file” in doc1 has an “absolutepath” attribute that is NOT the same as one or more “absolutepath” attributes in the corresponding “file” element in doc2?

simple example:

doc1:

<doc>
  <file path="c:\temp\A.xml">
    <link absolutepath="c:\temp\B.xml"/>
    <link absolutepath="c:\temp\C.xml"/>
  </file>
  <file path="c:\temp\C.xml"> <!--This should match, because it's child link absolutepath is not the same as child link absolutepath of the corresponding file with the same path in doc2-->
    <link absolutepath="c:\temp\D.xml"/>
    <link absolutepath="c:\temp\F.xml"/>
  </file>
</doc>

doc2:

<doc>
  <file path="c:\temp\A.xml">
    <link absolutepath="c:\temp\B.xml"/>
    <link absolutepath="c:\temp\C.xml"/>
  </file>
  <file path="c:\temp\C.xml">
    <link absolutepath="c:\temp\D.xml"/>
    <link absolutepath="c:\temp\E.xml"/>
  </file>
</doc>

Any ideas?

EDIT: Edited the example xml to show what I mean by multiple links for each file element. So what I want is each file in doc1 that has a link element with an absolutepath that is not found in a link element in doc2. So there are actually the same number of links in both, but the absolutepath may differ sometimes and that’s what I want to find and extract those files where there is such a difference in the link elements.

Here’s my attempt to modify the query suggested by Jon, to extract multiple links, but I think I’m doing it wrong, because I don’t get the correct result from the Except query afterwards:

   var files = from file in doc1.Descendants("file")

                select new
                {
                    file = file.Attribute("path").Value,
                    link = file.Elements("link").Attributes("absolutepath")
                };
    var oldfiles = from file in doc2.Descendants("file")
                from link in file.Elements("link")
                select new
                {
                    file = file.Attribute("path").Value,
                    link = file.Elements("link").Attributes("absolutepath")
                };
    //Get the ones that are different between them
    var missing = files.Except(oldfiles);
  • 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-24T20:15:25+00:00Added an answer on May 24, 2026 at 8:15 pm

    Well, I would start with the XML part. I originally made this more complicated than it needs to be, but I think you can just use:

    var files = from file in document.Descendants("file")
                from link in file.Elements("link")
                select new { file = file.Attribute("path").Value,
                             link = link.Attribute("absolutepath").Value };
    

    Then if you have files1 and files2 (the above query applied to each document) you can just do:

    var extraFiles = files1.Except(files2);
    

    EDIT: To get back to the link elements for those files, you can use:

    var linkElements = from link in file.Descendants("link")
                       join extra in extraFiles on 
                             new { file = link.Parent.Attribute("path").Value,
                                   link = link.Attribute("absolutepath").Value }
                             equals extra
                       select link;
    

    It’s a bit of a shame to query the document again, but there we go…

    (I’ve selected the link element rather than the file element so you can get to exactly the right bit – you can always choose the parent element to get to the file.)

    EDIT: Okay, if there are multiple link elements and you just want to find files with missing elements, that’s actually pretty easy from what we’ve got:

    var justFiles = new HashSet<string>(extraFiles.Select(x => x.file).Distinct());
    var fileElements = from element in file.Descendants("file")
                       where justFiles.Contains((string) element.Attribute("path")
                       select element;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can I query an xml file where I have multiple items with the
How can I write a query similar to this one in LINQ to SQL
I have a pretty complicated Linq query that I can't seem to get into
Can anyone help? I have a linq query which is embedded inside a extension
can anyone help?, i am stuck with a linq query.. basically i have a
I'm new to Linq and I'm trying to query a XML document to find
I have two tables A & B. I can fire Linq queries & get
Currently, I have this method to compare two numbers Private Function ETForGreaterThan(ByVal query As
I have two classes: Property and PropertyValue. A property has several values where each
I have two obects, A & B for this discussion. I can join these

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.