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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:57:45+00:00 2026-05-13T16:57:45+00:00

I am trying to query this very complicated XML document using xDocument and LINQ

  • 0

I am trying to query this very complicated XML document using xDocument and LINQ to XML. I want to perform the following action:

Get all elements that answer to a certain criteria, and if they don’t, return another attribute from the xDocument.

Example:

<cars>
    <car>
        <patrol type="oil">
            <url> http://Toyotaoil.com </url>
        </patrol>
    </car>
    <car>
        <patrol type="oil">
            <url> http://BMWoil.com </url>
        </patrol>
        <patrol type="gas">
            <url> http://BMWgas.com </url>
        </patrol>
    </car>
    <car>
        <patrol type="gas">
            <url> http://Hondagas.com </url>
        </patrol>
    </car>

Now what I’d like to get from this query is a list of patrols of type oil, unless the car doesn’t use petrol, and then I’d be satisfied with gas.

If I use the where clause I just miss the cases where the car uses gas. Is there any such thing like a where clause, where I can specify what to do if they condition wasn’t met?

  • 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-13T16:57:45+00:00Added an answer on May 13, 2026 at 4:57 pm

    The solution below should give you the flexibility to query whatever you like:

    var result = from car in xdoc.Element("cars").Elements("car") 
                 let patrols = car.Elements("patrol")
                 let oils = patrols.Where(patrol => patrol.Attribute("type") == "oil")
                 select new {
                        Car = car,
                        Patrols = (oils.Any() ? oils : patrols)
                 }
    

    I don’t have Visual Studio here, so I hope it compiles 🙂
    Give a bit more information on what you like to select, and I’ll give you a more specific LINQ statement.

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

Sidebar

Related Questions

I'm trying to perform a LINQ query on a DataTable object and bizarrely I
I am trying to execute this SQL query prior to restoring a .BAK file
I'm trying to optimize query performance and have had to resort to using optimizer
When trying to enter a SQL query with parameters using the Oracle OLE DB
I' trying to use a Linq query to find and set the selected value
I'm trying to perform a SQL query through a linked SSAS server. The initial
I'm trying out the following query: SELECT A,B,C FROM table WHERE field LIKE 'query%'
Before trying to query the AD server I would like to check if it
I'm trying to do query result pagination with hibernate and displaytag, and Hibernate DetachedCriteria
I'm trying write a query to find records which don't have a matching record

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.