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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:52:28+00:00 2026-05-31T13:52:28+00:00

Ok so I am new to C# but a very experienced developer in other

  • 0

Ok so I am new to C# but a very experienced developer in other langauges but i dont know how to handle if the NodeSelect is nill

 DirectoryInfo root = new DirectoryInfo(root_path);
 XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object. 
 xmlDoc.Load(root_path +  @"\file.xml"); //* load the XML document from the specified file. 
        //* Get elements.
  XmlNodeList elements = xmlDoc.SelectNodes("//elements");
  foreach (XmlNode node in elements){
    string link = node.SelectSingleNode("link").InnerText.Trim();

In the above example link may or maynot be in the element block in the xml and i need it to not give me this error

NULLReferenceExemption 
Object reference not set to an instance of an object.

I figured a try catch would work but i know there has got to be a better way in C#

UPDATE

            var linkNode = node.SelectSingleNode("link");
            if (linkNode != null)
            {
                string link = linkNode.InnerText.Trim();
            }
            Console.WriteLine("link: " + link);

error is Error

 The name 'link' does not exist in the current context
  • 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-31T13:52:30+00:00Added an answer on May 31, 2026 at 1:52 pm

    As you say, if the link node may or may not be there, you have to test for its existence before accessing its methods and properties.

    something in the sort of

    var ln = node.SelectSingleNode("link");
    if (ln != null && ln.InnerText!=null)
    {
       string link = ln.InnerText.Trim();
       ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to AD LDS and experienced but not qualified with SSAS, so
I am an experienced .NET developer but new to EF - so please bear
I'm a fairly experienced .NET desktop developer, but I'm new to Silverlight. I'm trying
I'm an experienced software developer, but pretty new to JS and to node. I'm
I'm pretty new to web design (but a very experienced programmer) and I'm creating
I'm new too Java, but I'm very experienced with PHP. When I was writing
I am a very experienced MS Sql developer, and a few new job positions
I'm an experienced AS developer, but this is something very basic about flex that
I'm new to IDEA, but very familiar with Eclipse. I followed the instructions here
I'm very new to Java but I've been developing a habit to use final

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.