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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:01:14+00:00 2026-06-18T10:01:14+00:00

I have a simple XML document that looks like this: <Person> <LastName>LastName1</LastName> <FirstName>FirstName1</FirstName> <MiddleName>MiddleName1</MiddleName>

  • 0

I have a simple XML document that looks like this:

<Person>
  <LastName>LastName1</LastName>
  <FirstName>FirstName1</FirstName>
  <MiddleName>MiddleName1</MiddleName>
  <Suffix>Suffix1</Suffix>
</Person>

However I have a constraint where I am not allowed to add empty tags. Therefore if the Suffix value does not exist, I cannot use <Suffix /> or validation will fail.

I’m composing the XML structure using XElement objects from different classes that return their respective XML via a returned XElement object from a .ToXML() method. I need to check per element to see if the XElement being returned is null. If that’s the case it has to be like that line never existed. I’m trying to use the ?? operator but I’m getting the error that ?? left operand is never null. I had the code as follows:

public XElement ToXML()
{
  return new XElement("Employee",
    new XElement(this.LastName.ToXML()) ?? null,
    new XElement(this.FirstName.ToXML()) ?? null,
    new XElement(this.MiddleName.ToXML()) ?? null,
    new XElement(this.Suffix.ToXML()) ?? null);
}

How can I check per XML node to see if the XElement object being returned is null and if so ignore adding/composing that node all together? Any help is appreciated, thanks!

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

    A constructor in C# will either return a non-null reference to the object or will throw an exception. It will not return null*.

    As for your problem, why not:

    return new XElement("Employee",
        this.LastName.ToXML(),
        this.FirstName.ToXML(),
        this.MiddleName.ToXML(),
        this.Suffix.ToXML());
    

    And just have each of those ToXML methods return null if none exist?

    Or if your case is the properties themselves are null:

    return new XElement("Employee",
        this.LastName != null ? this.LastName.ToXML() : null, /* null is ignored */
        this.FirstName != null ? this.FirstName.ToXML() : null,
        this.MiddleName != null ? this.MiddleName.ToXML() : null,
        this.Suffix != null ? this.Suffix.ToXML() : null);
    

    I also just realized perhaps you always get back an XElement, but it may be empty, in that case:

    var elements = new[] { this.LastName.ToXML(), this.FirstName.ToXML(), ...
    
    // use IsEmpty to filter out those that are <Element/>
    return new XElement("Employee",
        elements.Where(ee => ee != null && !ee.IsEmpty));
    

    *I believe there is an interesting edge case where you could get this from a COM interface instantiation, but we’ll ignore all “strange” coding.

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

Sidebar

Related Questions

I have an xml document that looks something like this: <units> <unit> <year></year> <month></month>
I have a XML document that looks something like this: <?xml version=1.0 encoding=UTF-8?> <citizen>
I have a simple xml file that looks like this: <?xml version=1.0 encoding=UTF-8 standalone=yes
I have a simple xml document that looks like the following snippet. I need
I have a simple XML file that looks like this: <?xml version=1.0 ?> <devices>
I have an XML document with (basically) looks like this: ... <param> <key>age</key> <value>10</value>
Situation: I have a simple XML document that contains image information. I need to
I have a very simple XML document that I've retrieved from a larger parent.
I've got a 2-part question... I have some XML that looks like: <trans-unit id=70
I have a simple XML extraction issue that should be solvable with straight PHP

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.