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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:33:33+00:00 2026-05-10T22:33:33+00:00

Is there an attribute to skip empty arrays in the xml-serialization of c#? This

  • 0

Is there an attribute to skip empty arrays in the xml-serialization of c#? This would increase human-readability of the xml-output.

  • 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. 2026-05-10T22:33:34+00:00Added an answer on May 10, 2026 at 10:33 pm

    Well, you could perhaps add a ShouldSerializeFoo() method:

    using System; using System.ComponentModel; using System.Xml.Serialization; [Serializable] public class MyEntity {     public string Key { get; set; }      public string[] Items { get; set; }      [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]     public bool ShouldSerializeItems()     {         return Items != null && Items.Length > 0;     } }  static class Program {     static void Main()     {         MyEntity obj = new MyEntity { Key = 'abc', Items = new string[0] };         XmlSerializer ser = new XmlSerializer(typeof(MyEntity));         ser.Serialize(Console.Out, obj);     } } 

    The ShouldSerialize{name} patten is recognised, and the method is called to see whether to include the property in the serialization. There is also an alternative {name}Specified pattern that allows you to also detect things when deserializing (via the setter):

    [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)] [XmlIgnore] public bool ItemsSpecified {     get { return Items != null && Items.Length > 0; }     set { } // could set the default array here if we want } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to deploy an item without using this attribute ? I'm
Is there any attribute or other method of getting T4 to skip trying to
When publishing an RSS feed, is there some attribute in the XML that can
Is there XML attribute that does the exact opposite of android:dependency ? What I
Is there any attribute associated with the starting time I can get from the
In JPA there is an attribute called referencedColumnName that can be set on @JoinColumn,
There is no align attribute with font tag because It is an inline element.
Is there a MSBuild task for setting custom attribute in AssemblyInfo.cs? I found AssemblyInfo
Is there a pragma or debugger attribute which will allow the debugger to not
Is there a way to override an attribute setter on activeresource when the object

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.