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

  • Home
  • SEARCH
  • 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 8507983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:01:39+00:00 2026-06-11T03:01:39+00:00

I have a class containing an array I wish to serialize with XmlSerializer :

  • 0

I have a class containing an array I wish to serialize with XmlSerializer:

[XmlArray("properties")]
[XmlArrayItem("property", IsNullable = true)]
public List<Property> Properties { get; set; }

Property is a class containing an attribute and some XmlText:

[XmlAttribute("name")]
public string Name { get; set; }

[XmlText]
public string Value { get; set; }

The problem is that when Value is null, it serializes as an empty string:

<property name="foo" />

rather than a null. I’m looking for the value to either be omitted entirely, or look like this:

<property name="foo" xsi:nil="true" />

Is it possible to null out an element in a list based on its XmlText value? I’m really trying to avoid custom serialization, but perhaps some other serialization framework would be better in this case?

  • 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-11T03:01:41+00:00Added an answer on June 11, 2026 at 3:01 am

    Use the IsNullable=true in the XmlArrayItemAttribute class. For an example.

    [XmlRoot("Root")]
    public class Root
    {
        [XmlArrayItem("Element", IsNullable = true)]
        public string[] Elements { get; set; }
    }
    

    Some sample code in Visual Studion 2012 and .Net 4.5:

    using System.Xml.Serialization;
    
    ...
    
    // Test object
    Root root;
    root = new Root();
    root.Elements = new string[] { null, "abc" };
    
    using(MemoryStream stream = new MemoryStream())
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(Root));
        xmlSerializer.Serialize(stream, root);
    
        Console.WriteLine(new string(Encoding.UTF8.GetChars(stream.GetBuffer())));
    }
    

    The output is (line breaks added for clarity):

    <?xml version="1.0"?>
    <Root 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Element>
        <string xsi:nil="true" />
        <string>abc</string>
      </Element>
    </Root>
    

    And with a complex type (also in .Net 4.5 on Visual Studio 2012):

        public class MyProperty
        {
            public string Foo { get; set; }
        }
    
        [XmlRoot("Root")]
        public class Root
        {
            [XmlArrayItem("Element", IsNullable = true)]
            public MyProperty[] Elements { get; set; }
        }
    
        ,,,
    
        Root root;
        root = new Root();
        root.Elements = new MyProperty[] { null, new MyProperty{ Foo = "bar" } };
    
        // Other code is as above
    

    Using the same code above produces:

    <?xml version="1.0"?>
    <Root 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Elements>
        <Element xsi:nil="true" />
        <Element>
          <Foo>bar</Foo>
        </Element>
      </Elements>
    </Root>
    

    Also remember that the type must be a reference type (not a struct, for example) to write out xsi:nil=true.

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

Sidebar

Related Questions

I have a class containing several properties (all are strings if it makes any
I have an example class containing two data points: public enum Sort { First,
I have a list of items, where each item is a simple class containing
I have a simple jar file containing class A : public class A {}
I have been trying to create an array of a class containing two values,
I have a un-ordered list containing links with the same class and a unique
Possible Duplicate: Java Generics: Array containing generics I have a Java class which contains
I have a class CField and a class CBoard containing a 2d array of
I have a 2D array containing many instances of a class. The class contains
I have a class containing a collection of items. For convenience I've provided GetCurrentItem

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.