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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:00:41+00:00 2026-05-18T10:00:41+00:00

I am using a chain of calls to the XElement.Element() method to drill down

  • 0

I am using a chain of calls to the XElement.Element() method to drill down into an XML document and pull out the value of an attribute:

XElement root = ...;
XNamespace ns = "...";
var firstName = 
  root
    .Element(ns + "Employee")
    .Element(ns + "Identity")
    .Element(ns + "Name")
    .Attribute(ns + "FirstName");

However, since the incoming document has not been schema validated it is possible that a malformed document will cause a NullReferenceException if any of the expected intermediate elements does not exist.

Is there a way to avoid this risk, whilst keeping the code succinct?

I can wrap the code above in a handler for NullReferenceException however this feels wrong, and would also not specifically indicate where the failure occurred. Constructing an informative error message would be manual, tedious, error prone, and a maintenance hazard.

Should I be using XPath instead, that way I can check for a null return and then easily construct an error message indicating that the XPath expression could not be resolved?

  • 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-18T10:00:42+00:00Added an answer on May 18, 2026 at 10:00 am

    One option is to use Elements() instead of Element() – which will yield an empty sequence if the element isn’t found. Using the extension methods in Extensions, you can go from a sequence of elements to a sequence of elements – and the same for attributes. So:

    var firstName = 
      root
        .Elements(ns + "Employee")
        .Elements(ns + "Identity")
        .Elements(ns + "Name")
        .Attributes(ns + "FirstName")
        .FirstOrDefault();
    

    There is a difference between the two snippets, mind you – this will find the first matching attribute even if it comes from (say) the first Name element within the third Identity element within the second Employee element. That may or may not be an issue for you.

    (Just to check, do you definitely need the namespace on the attribute? Unlike elements, attributes don’t inherit a “default” namespace. It’s rarer to use attributes on namespaces than on elements.)

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

Sidebar

Related Questions

I'm trying to create a chain of function calls using two objects. I've added
I have a chain of async calls, linked together using dojo.Deferred objects as async
I am using the Task Parallel Library to set up a chain of Tasks
Today I'm working on a pet project using chained function calls, and I'm curious
Class1 creates and calls a method in Class2. Class2's method updates it progress to
I know that you can't call a method using a filter because it does
In the old days, I used to chain calls to live() with great success,
When using jQuery functions like replaceWith , the function return-value is (as clearly documented),
I made a simple clock signal using goroutines: func clockloop(ch chan byte) { count
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain

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.