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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:42:34+00:00 2026-06-12T22:42:34+00:00

I have XML as shown below <NodeName Foo=True Foobar=False /> <NodeName Foo=False Foobar=False />

  • 0

I have XML as shown below

<NodeName Foo="True" Foobar="False" />
<NodeName Foo="False" Foobar="False" />
<NodeName Foo="True" Foobar="False" />

I am querying this to find NodeName’s with a Foo value of ‘True’ using:

.Where(node => node.Attribute("Foo").Value = "True");

However there is the possibility that the attribute Foo may not exist and this causes an exception.

The question is how do you implement logic to check if the attribute exists and then check for the value if it does exist?

I have tried the following however not sure what goes in the else bracket to return nothing as currently it wont compile with an error of not returning a value on all paths.

.Where(node =>
            {
                if (node.Attribute("Foo") != null)
                {
                    node.Attribute("Foo").Value == "True";
                }
                else { }
            });
  • 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-06-12T22:42:35+00:00Added an answer on June 12, 2026 at 10:42 pm

    However there is the possibility that the attribute Foo may not exist and this causes an exception.

    This is where the explicit conversion from XAttribute (and indeed XElement) to string and a number of other types comes in handy:

    .Where(node => (string) node.Attribute("Foo") == "True");
    

    The conversion will return null when there’s no such attribute. This is much more convenient than finding the attribute twice – once for presence and once for the value.

    Alternatively, you could use the conversion to bool? which will also return null if the attribute is missing, so you need to use the null-coalescing operator to effectively provide a default:

    .Where(node => (bool?) node.Attribute("Foo") ?? false);
    

    EDIT: In terms of why your statement-lambda approach doesn’t work, you don’t have any return statements. This would work, although obviously it’s not what I’d recommend:

    .Where(node =>
            {
                if (node.Attribute("Foo") != null)
                {
                    return node.Attribute("Foo").Value == "True";
                }
                else
                {
                    return false;
                }
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an xml file sitemap.xml as shown below ..i need to add one
I have a bit of xml file named Sample.xml which is shown below <?xml
We have XML such as shown below. We have a corresponding Team, Players, and
I have a XML file with the structure as shown below: <x> <y/> <y/>
Simple problem: With the XML layout shown below, I have some views wrapped in
I have XML similar to the following <?xml version=1.0 encoding=utf-8?> <foo name=FooBar xmlns=http://mydomain/myapp/ver/myschema.xsd> <bars
I have a XML document like shown below: <root> <level1> <level2> <level3> ..... </level3>
I’m using Linq to extract values from some XML. Shown below is a simplified
Say I have an xml document consisting of a list as shown below: <Items>
I have a simple XML file as shown below. <?xml version=1.0 encoding=UTF-8?> <myConfig> <datasets>

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.