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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:34:39+00:00 2026-05-17T19:34:39+00:00

My question is regarding conditionally creation of XElements, that is, if some condition is

  • 0

My question is regarding conditionally creation of XElements, that is, if some condition is met, create the XElement, if not, skip creating the XElement? At this point of time, I could create empty XElements, and then remove all empty elements, by checking, if IsEmpty is true, but that somehow does not feel right…

I feel, that a small example might be in order:

XDocument doc = new XDocument(new XDeclaration("1.0","utf-8","yes"),
new XElement("Books",
    new XElement("Book", new XElement("Title", "Essential LINQ"), new XElement("Author", "Charlie Calvert,Dinesh Kulkarni")),
    new XElement("Book", new XElement("Title", "C# in Depth"), new XElement("Author", "Jon Skeet")),
    new XElement("Book", new XElement("Title", "Some Title"), new XElement("Author", ""))
    ));

Imagine, that the “Author”-element is an optional element, and if we do not know the author, we simply do not put that element in the XML – the simple, and in my opinion, ugly solution, is to create the element, as an empty element, and remove it afterwards.

Anyone know how to make an elegant solution, so to say something like this:

condition_met ? new XElement("Author",variable_with_value) : do not create element

Best regards and feel free to ask for further information, if needed.

  • 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-17T19:34:39+00:00Added an answer on May 17, 2026 at 7:34 pm

    Use the fact that nulls are skipped in construction:

    condition_met ? new XElement("Author", variable_with_value) : null
    

    (LINQ to XML is full of neat little design decisions like this that make it a pleasure to work with.)

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

Sidebar

Related Questions

I have a question regarding interfaces. I understood that you cannot create instances of
The question is about a post regarding conditional variable that i glanced over. condition
Question regarding fluent nHibernate table mappings: Apparently I was under the mistaken impression, that
simple question regarding property value inheritance in UserControls. If i create a UserControl, it
A quick question regarding the java.lang.VerifyError exception. Suppose I get an error that looks
Question regarding how to setup dbase relationships (newbie, this may be trivial) Followed the
(except for proxy setup!) I spent some time writing a question here regarding a
My intention in this question is not to be pedantic, but rather to explore
While answering this question regarding safe escaping of filename with spaces (and potentially other
Quick question regarding this Socket(String host, int port) If I use these arguments in

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.