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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:49:23+00:00 2026-05-13T13:49:23+00:00

I have a class with some collections in them, and I would like to

  • 0

I have a class with some collections in them, and I would like to serialize instances of this class to XML without having to initialize the collections to be empty, and without having to implement IXmlSerializable. I don’t care if it creates empty elements, or doesn’t create the elements at all. Just that it works without having to initialize a collection for each collection based property.

I have look at all the XML attributes I can decorate the properties with, and have not had any success with this. This seems like a simple thing to do that is can have an element or just none at all. Then when it is being deserialized it would just leave them null or ignore them period.

Here is a simple version of a class to use for working through this issue. Using this and the defaults you get an exception “Object reference not set to an instance of an object” due to the collections being null;

public class MyClass
{
    public string Name { get; set; }
    public bool IsAlive { get; set; }
    public List<Car> Cars { get; set; }
    public List<Home> Homes { get; set; }
    public List<Pet> Pets { get; set; }

    public void ToXmlFile(string fileName)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
        TextWriter writer = new StreamWriter(fileName);
        serializer.Serialize(writer, this);
        writer.Close();
    }
}

EDIT
Thanks for the helps guys, it turns out the issue was in my GetHashCode method which didn’t handle the null correctly. Once I fixed this all was good. I marked the first one to answer as being correct. Sorry for the Red Herring, but working through it with you guys did help.

  • 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-13T13:49:23+00:00Added an answer on May 13, 2026 at 1:49 pm

    You do not need to initialize collections in order to serialize the class to XML. Here’s a simple program to demonstrate:

    class Program
    {
        static void Main(string[] args)
        {
            MyClass c = new MyClass() { Name = "Test", IsAlive = true };
            XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.Serialize(ms, c);
                Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
            }
            Console.ReadLine();
        }
    }
    

    This will print the following output:

    <?xml version="1.0"?>
    <MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Name>Test</Name>
      <IsAlive>true</IsAlive>
    </MyClass>
    

    In other words, null collection properties behave the same way as any other null property – they don’t get serialized at all (unless you change the default value, which I haven’t).

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

Sidebar

Related Questions

So I have a class is something like this public class Order { //some
I have some class that I'm passing as a result of a service method,
I have some class SimpleButton . It is a Child of Button class. Button
Suppose I have some class which has a property actor_ of type Actor .
I have a class with some method that depend by one parameter. What is
So I have some class starting with #include <wchar.h> #include <stdlib.h> and there is
Basically I have some class objects, each with three properties. Once one class object
If I have a class with some value member that I want to store
the basic idea is that you have some class that has a reference type
For example, I have some class hierarchy (possibly, with all kinds of inheritance -

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.