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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:31:55+00:00 2026-06-17T12:31:55+00:00

I have wrote a c# function in order to parse an XML Stream. My

  • 0

I have wrote a c# function in order to parse an XML Stream.
My XML can have several nodes.

Example :

<Stream>
 <One>nnn</One>
 <Two>iii</Two>
 <Three>jjj</Three>
</Stream>

But sometimes, it is :

<Stream>
 <Two>iii</Two>
</Stream>

Here is my c# code :

var XML = from item in XElement.Parse(strXMLStream).Descendants("Stream") select item;
string strOne = string.Empty;
string strTwo = string.Empty;
string strThree =  string.Empty;

if ((item.Element("One").Value != "")
{
   strOne = item.Element("One").Value;
}

if ((item.Element("Two").Value != "")
{
   strTwo = item.Element("Two").Value;
}

if ((item.Element("Three").Value != "")
{
   strThree = item.Element("Three").Value;
}

With this code, if my Stream is full ( Node On, Two and three), there’s no problem! But, if my Stream has only the node “Two”, I get a NullReferenceException.

Is there a way to avoid this exception (I cannot change my Stream).

Thanks a lot 🙂

  • 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-17T12:31:56+00:00Added an answer on June 17, 2026 at 12:31 pm

    You should check if item.Element("anything") is null before accessing it’s Value property.

    if (item.Element("Three") != null && item.Element("Three").Value != "")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

guys, I wrote a function to test if two inputs (a and b) have
I have a C++ DLL that I wrote that has a single exposed function,
I wrote a function in Python 2.7 (on Window OS 64bit) in order to
I have following WP function and shortcode I wrote function selected_inventory_products( $atts ) {
I have a page that is getting an XML document in order to add
I wrote a simple function to create a hashtable out of an xml-file which
I have wrote a wrapper through FFI for a shared library function(third party function).
I have this Array i wrote a function MostFreq that takes an array of
I have to write a swap function for my bubble sort That's what I've
I have a simple file read and write function. private void WriteToFile(String filename, String

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.