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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:27:59+00:00 2026-05-10T18:27:59+00:00

Lately I’ve been using XPathDocument and XNavigator to parse an XML file for a

  • 0

Lately I’ve been using XPathDocument and XNavigator to parse an XML file for a given XPath and attribute. It’s been working very well, when I know in advance what the XPath is.

Sometimes though, the XPath will be one of several possible XPath values, and I’d like to be able to test whether or not a given XPath exists.

In case I’m getting the nomenclature wrong, here’s what I’m calling an XPath – given this XML blob:

<foo>     <bar baz='This is the value of the attribute named baz'> </foo> 

I might be looking for what I’m calling an XPath of ‘//foo/bar’ and then reading the attribute ‘baz’ to get the value.

Example of the code that I use to do this:

XPathDocument document = new XPathDocument(filename); XPathNavigator navigator = document.CreateNavigator(); XPathNavigator node = navigator.SelectSingleNode('//foo/bar'); if(node.HasAttributes) {     Console.WriteLine(node.GetAttribute('baz', string.Empty)); } 

Now, if the call to navigator.SelectSingleNode fails, it will return a NullReferenceException or an XPathException. I can catch both of those and refactor the above into a test to see whether or not a given XPath returns an exception, but I was wondering whether there was a better way?

I didn’t see anything obvious in the Intellisense. XPathNavigator has .HasAttributes and .HasChildren but short of iterating through the path one node at a time, I don’t see anything nicer to use.

  • 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. 2026-05-10T18:27:59+00:00Added an answer on May 10, 2026 at 6:27 pm

    If you’ve given valid XPath but it doesn’t match anything, SelectSingleNode won’t throw a NullReferenceException – it will just return null.

    If you pass SelectSingleNode some syntactically invalid XPath, that’s when it will throw an XPathException.

    So normally, you’d just need to test whether the returned value was null or not.

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

Sidebar

Ask A Question

Stats

  • Questions 112k
  • Answers 112k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, there's your problem ;) It looks like you're calling… May 11, 2026 at 9:57 pm
  • Editorial Team
    Editorial Team added an answer I've had to do this task before on some very… May 11, 2026 at 9:57 pm
  • Editorial Team
    Editorial Team added an answer You have this line of code:- String sSentSearchText = postedValues[i];… May 11, 2026 at 9:57 pm

Related Questions

Lately I had to change some code on older systems where not all of
Lately I've be moving source files around in our source tree. For example placing
Lately I've been in the habit of assigning integer values to constants and simply
Lately I've been using XPathDocument and XNavigator to parse an XML file for a
Lately I have seen a lot of blog posts concerning how to build loosely

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.