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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:37:46+00:00 2026-05-13T14:37:46+00:00

How can I do this? Or will the serializer automatically go with recursion, and

  • 0

How can I do this? Or will the serializer automatically go with recursion, and serialize all those child objects into XML?

Give me an example how would you serialize classes that contain other classes’ objects in themselves! That was the core of this question!

I’ve tried this, and it didn’t output anything (except the XML header) to the targeted XML file.

My problem is that I need to serialize a simple class, that just holds a List object. But, those Entities also hod List objects. (Another plus would be if I could avoid the serialization of some components, because some are derived and have dictionaries in them).

public void SaveCurrent(string MapFileName)
{
    string MapPath = world_.game_.Content.RootDirectory + "/Maps/" + MapFileName + ".xml";
    StreamWriter MapWriter = new StreamWriter(MapPath);

    Map SavedMap = new Map();
    SavedMap.Entities = world_.Entities;
    XmlSerializer xSerializer = new XmlSerializer(SavedMap.GetType());

    xSerializer.Serialize(MapWriter, SavedMap);
    MapWriter.Close();
}

That’s the piece of code that does the serialization.

public class Map
{
    internal string MapName;
    internal string MapDescription;
    internal string MapAuthor;
    public List<Entity> Entities = new List<Entity>();
}

And this is the class that’s serialized. Could internals be counted as publics, if the serialization is called from the same assembly? The code throws exception at the SavedMap.GetType() function, and I’ve tried typeof(Map) too, but without success. I guess it’s because I need some other way to handle each new class (deep serialization) how do I do that?

Also, I’ve found on some examples, that there are no interface inheritance or attributes, therefore I didn’t add those either, but I’m planning to use IXmlSerializable, though I don’t know how to call another serialization inside WriteXML implementation.

  • 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-13T14:37:46+00:00Added an answer on May 13, 2026 at 2:37 pm

    About the type problem that Josh Einstein mentionned, you do not have to work with the XmlInclude attribute : you can also pass the list of types to the serializer (signature being XmlSerializer(Type baseType, Type[] extraTypes)). This should be done especially if there’s a chance that the extra types list grow over time.

    Finding the extra-types can be either done through reflection over the object to be serialized or reflection at startup on the loaded assemblies to fetch any needed types.

    EDIT : raw example :

    public abstract class Animal
    {
    }
    
    public class Dog : Animal
    {
    }
    
    public class Cat : Animal
    {
    }
    
    public static class AnimalSerializer
    {
        public static void Serialize(List<Animal> animals, Stream stream)
        {
            List<Type> animalTypes = new List<Type>();
            foreach (Animal animal in animals)
            {
                Type type = animal.GetType();
                if (!animalTypes.Contains(type))
                {
                    animalTypes.Add(type);
                }
            }
            XmlSerializer serializer = new XmlSerializer(typeof(List<Animal>), animalTypes.ToArray());
            serializer.Serialize(stream, animals);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Save An onject into a xml file with serialize like this: FileStream stream
I am trying to serialize my object to xml. A serializer seemingly serializes all
This will probably be obvious but I can't find the best way. I want
I aware that this will be a less programming question, but still... How can
ATM i cant quiet imagine how this will work. I'm sure it can be
from this code will show in original color how can i change it to
Can anyone please suggest me whether this code will cause concurrency or not. This
Can anyone tell by looking at this style will cause a combobox with it
This question will be short and sweet. I know an instruction can occur between
The criteria is basically this: folders will exist for about 24-48hrs folders names can

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.