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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:21:16+00:00 2026-05-23T06:21:16+00:00

Given something like this: var results = theElement.Element(Blah).Element(Whatever).Elements(Something); Is there an elegant way to

  • 0

Given something like this:

var results = theElement.Element("Blah").Element("Whatever").Elements("Something");

Is there an elegant way to deal with a null Blah or Whatever element so results is just null or empty in these cases?

I know I can split up the query and manually do these checks but was wondering if there was something more succinct.

  • 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-23T06:21:16+00:00Added an answer on May 23, 2026 at 6:21 am

    You can add some extension methods to do this for you. For the Element method you would return null or the element itself. For the Elements method you would return an empty result or the target elements.

    These are the extension methods:

    public static class XElementExtensions
    {
        public static XElement ElementOrDefault(this XElement element, XName name)
        {
            if (element == null)
                return null;
    
            return element.Element(name);
        }
    
        public static IEnumerable<XElement> ElementsOrEmpty(this XElement element, XName name)
        {
            if (element == null)
                return Enumerable.Empty<XElement>();
    
            return element.Elements(name);
        }
    }
    

    You can use them in this manner:

    var query = theElement.ElementOrDefault("Blah")
                          .ElementOrDefault("Whatever")
                          .ElementsOrEmpty("Something");
    if (query.Any())
        // do something
    else
        // no elements
    

    If you’re not querying for ElementsOrEmpty and your last request is for ElementOrDefault you would check for null instead of using the Enumerable.Any method.

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

Sidebar

Related Questions

There's something very unsatisfactory about this code: /* Given a command string in which
I have a Linq query that looks something like this: var myPosse = from
I'm trying something like this Output.py print Hello Input.py greeting = raw_input(Give me the
Given [1,2,3,4,5] , how can I do something like 1/1, 1/2, 1/3,1/4,1/5, ...., 3/1,3/2,3/3,3/4,3/5,....
For example, given a type param method i'm looking for something like the part
Is there an open source alternative to SharePoint? I'd like something that gives the
This was something originally discussed during a presentation given by Charles Brian Quinn of
Lets say I have a class like this: Class User { var $id var
Given a child element, is there a simple means of determining how many parent
My classes look something like this (I include only the relevant properties): public class

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.