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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:41:18+00:00 2026-06-08T07:41:18+00:00

I am generating XML documents documents using LINQ to XML. I want the XML

  • 0

I am generating XML documents documents using LINQ to XML. I want the XML document to be minimal, i.e. seldom used properties should be omitted. Currently, I am doing it like this:

XElement element = new XElement("myelement",
        new XAttribute("property1", value1),
        new XAttribute("property2", value2));
if (!string.IsNullOrEmpty(rareValue1))
{
    element.Add(new XAttribute("rareProperty1", rareValue1));
}
if (!string.IsNullOrEmpty(rareValue2))
{
    element.Add(new XAttribute("rareProperty2", rareValue2));
}
if (!string.IsNullOrEmpty(rareValue3))
{
    element.Add(new XAttribute("rareProperty3", rareValue3));
}

But actually, if would like to omit the “if”-statements, because they are not very elegant and they contradict the LINQ to XML philosophy where you can easily create XML trees by nesting as described in Creating Trees in XML.
So, I would like to do something like this:

XElement element = new XElement("myelement",
        new XAttribute("property1", value1),
        new XAttribute("property2", value2),
        new XAttribute("rareProperty1", string.IsNullOrEmpty(rareValue1) ? Flag.Omit : rareValue1),
        new XAttribute("rareProperty2", string.IsNullOrEmpty(rareValue2) ? Flag.Omit : rareValue1),
        new XAttribute("rareProperty3", string.IsNullOrEmpty(rareValue3) ? Flag.Omit : rareValue1),
);

I.e. the C# source code contains all child attributes of myelement inside its constructor. And Flag.Omit would be some way to instruct LINQ-to-XML not to generate an XML attribute.

Is this possible with standard LINQ to XML or with some generic utility function?

  • 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-08T07:41:21+00:00Added an answer on June 8, 2026 at 7:41 am

    The various ways of adding child nodes all ignore null values – so all you need is a helper method:

    public static XAttribute AttributeOrNull(XName name, string value)
    {
        return string.IsNullOrEmpty(value) ? null : new XAttribute(name, value);
    }
    

    Then:

    XElement element = new XElement("myelement",
            new XAttribute("property1", value1),
            new XAttribute("property2", value2),
            AttributeOrNull("rareProperty1", rareValue1),
            AttributeOrNull("rareProperty2", rareValue2),
            AttributeOrNull("rareProperty3", rareValue3)
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

we're currently generating all our official documents using XSL-FO transformation using .xml files as
I want to add doctypes to my XML documents that I'm generating with LXML's
I'm generating a table in a pdf document using xml-fo. The number of columns
Background: I am generating pieces of a much larger XML document (HL7 CDA documents)
I'm generating an XML document in VBA using the MSXML2.DOMDocument and then sending the
I'm generating an XML document in parts using various transforms. In particular, I'm generating
I'm generating a large amount of XML documents from a set of values in
From my VB application I am generating XML document based on data fetched from
I am generating an XML document from a StringBuilder, basically something like: string.Format(<text><row>{0}</row><col>{1}</col><textHeight>{2}</textHeight><textWidth>{3}</textWidth><data>{4}</data><rotation>{5}</rotation></text> Later,
I am generating an XML file from a VB.NET app. The document was generating

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.