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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:59:36+00:00 2026-05-12T17:59:36+00:00

I’m trying to teach-myself how to serialize/deserialize to/from XML using the attribute-based serializer. I’ve

  • 0

I’m trying to teach-myself how to serialize/deserialize to/from XML using the attribute-based serializer. I’ve put the below code together for testing purposes but it seems that I might have missed a point or two. Can anyone assist me and tell me what to do to have the whole damn thing work properly? The code below should compile and run just fine but will throw an exception – something is probably wrong with my attributes.

What did I miss?

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;

namespace XMLSerialisation_test
{
    class Program
    {
        static void Main(string[] args)
        {
            World my_world = new World(new Point(20, 30));

            for (int i = 0; i < 10; i++)
            {
                string property = String.Format("Property no.{0}", i);
                my_world.PushWorldObject(new MyObject(new Point(i, i), property));
            }

            DataContractSerializer world_serializer = new DataContractSerializer(typeof(World));

            try
            {
                using (Stream s = File.Create("output.xml"))
                    world_serializer.WriteObject(s, my_world);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occured : {0}", e.Message);
            }

        }
    }
}

WorldObject.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace XMLSerialisation_test
{
    [DataContract]
    public struct Point // couldn't find the pre-defined Point for some reason
    {
        public Point(double x, double y)
        { X = x; Y = y; }
        [DataMember]
        public double X;
        [DataMember]
        public double Y;
    }

    [DataContract]
    public abstract class WorldObject
    {
        public WorldObject() : this(0.0, 0.0)
        {}

        public WorldObject(Point loc)
        { m_location = loc; }

        public WorldObject(double x, double y)
        { 
            m_location.X = x;
            m_location.Y = y;
        }

        [DataMember]
        public Point Location
        {
            get { return m_location; }
            set { m_location = value; }
        }

        protected Point m_location;
    }

    [DataContract]
    public class MyObject : WorldObject
    {
        public MyObject(string prop)
            : base(0.0, 0.0)
        { m_property = prop; }

        public MyObject(Point p, string prop)
            : base(p)
        { m_property = prop; }

        [DataMember]
        public string Property
        {
            get{ return m_property; }
            set{ m_property = value; }
        }

        private string m_property;
    }
}

World.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace XMLSerialisation_test
{
    [DataContract]
    class World
    {
        public World() : this(new Point(10, 10))
        { }

        public World(Point size)
        { m_world_size = size; }

        public bool PushWorldObject(WorldObject o)
        {
            try
            {
                WorldObjects.Add(o);
                return true;
            }
            catch(Exception e)
            {
                Console.WriteLine("Exception occured : {0}", e.Message);
                return false; }
        }


        #region Accessors
        [DataMember]
        public List<WorldObject> WorldObjects
        {
            get { return m_world_objects; }
            set { m_world_objects = value; }
        }
        [DataMember]
        public Point WorldSize
        {
            get { return m_world_size; }
            private set { m_world_size = value; }
        }
        #endregion

        #region Fields
        private List<WorldObject> m_world_objects = new List<WorldObject>();
        private Point m_world_size;
        #endregion
    }
}
  • 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-12T17:59:36+00:00Added an answer on May 12, 2026 at 5:59 pm

    Try this:

    DataContractSerializer world_serializer = new DataContractSerializer(typeof(World), new List<Type> { typeof(MyObject) });
    

    The problem is that PushWorldObject takes type WorldObject, but you are actually passing type MyObject. The serializer knows nothing about this type, so throws an exception. WorldObject is used within the World class, so this type is by known by default. However, MyObject is not used inside World – so you manually have to declare it as Known.

    As an alternative, you can add the KnownType attribute like so:

    [DataContract, KnownType(typeof(MyObject))]
    class World
    {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 207k
  • Answers 207k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Couldn't find a C# one but this Java one has… May 12, 2026 at 9:27 pm
  • Editorial Team
    Editorial Team added an answer I believe the problem is with this line: $this->headers[] =… May 12, 2026 at 9:27 pm
  • Editorial Team
    Editorial Team added an answer The 'm' in mDNS stands for "multicast." An mDNS query… May 12, 2026 at 9:27 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.