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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:49:33+00:00 2026-05-11T16:49:33+00:00

I’m trying to serialize and deserialize a tree of Node objects. My abstract Node

  • 0

I’m trying to serialize and deserialize a tree of Node objects. My abstract “Node” class as well as other abstract and concrete classes that derive from it are defined in my “Informa” project. In addition, I’ve created a static class in Informa for serialization / deserialization.

First I’m deconstructing my tree into a flat list of type Dictionary(guid,Node) where guid is the unique id of Node.

I am able to serialize all my Nodes with out a problem. But when I try to deserialize I get the following exception.

Error in line 1 position 227. Element
‘http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value‘
contains data of the
‘Informa:Building’ data contract. The
deserializer has no knowlege of any
type that maps to this contract. Add
the type corresponding to ‘Building’
to the list of known types – for
example, by usying the
KnownTypeAttribute or by adding it to
the list of known types passed to
DataContract Serializer.

All classes that derive from Node, including Building, have the [KnownType(typeof(type t))] attribute applied to them.

My serialization and deserialization methods are below:

public static void SerializeProject(Project project, string filePath)
{
    try
    {
        Dictionary<Guid, Node> nodeDic = DeconstructProject(project);

        Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);

        //serialize

        DataContractSerializer ser = new DataContractSerializer(typeof(Dictionary<Guid, Node>),"InformaProject","Informa");

        ser.WriteObject(stream,nodeDic);

        // Cleanup
        stream.Close();
    }
    catch (Exception e)
    {
        MessageBox.Show("There was a problem serializing " + Path.GetFileName(filePath) + ". \n\nException:" + e.Message, "Doh!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        throw e;
    }

}



public static Project DeSerializeProject(string filePath)
{
    try
    {
        Project proj;

        // Read the file back into a stream
        Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

        DataContractSerializer ser = new DataContractSerializer(typeof(Dictionary<Guid, Node>), "InformaProject", "Informa");

        Dictionary<Guid, Node> nodeDic = (Dictionary<Guid, Node>)ser.ReadObject(stream);

        proj = ReconstructProject(nodeDic);        

        // Cleanup
        stream.Close();

        return proj;

    }
    catch (Exception e)
    {
        MessageBox.Show("There was a problem deserializing " + Path.GetFileName(filePath) + ". \n\nException:" + e.Message, "Doh!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return null;
    }

}
  • 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-11T16:49:34+00:00Added an answer on May 11, 2026 at 4:49 pm

    All classes that derive from Node,
    including Building, have the
    [KnownType(typeof(type t))] attribute
    applied to them.

    KnownType is usually applied to the base type – i.e.

    [DataContract, KnownType(typeof(Building)), ...]
    abstract class Node { ... }
    

    (note – you can also specify the known-types in the DataContractSerializer constructor, without requiring attributes)

    EDIT RE YOUR REPLY

    If the framwork class doesn’t know about all the derived types, then you need to specify the known types when creating the serializer:

    [DataContract] abstract class SomeBase { }
    [DataContract] class Foo : SomeBase { }
    [DataContract] class Bar : SomeBase { }
    ...
    // here the knownTypes argument is important
    new DataContractSerializer(typeof(SomeBase),
          new Type[] { typeof(Foo), typeof(Bar) });
    

    This can be combined with (for example) preserveObjectReferences etc by replacing the null in the previous example.

    END EDIT

    However, without something reproducible (i.e. Node and Building), it is going to be hard to help much.

    The other odd thing; trees structures are very well suited to things like DataContractSerializer – there is usually no need to flatten them first, since trees can be trivially expressed in xml. Do you really need to flatten it?


    Example:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Xml;
    
    [DataContract, KnownType(typeof(Building))]
    abstract class Node {
        [DataMember]
        public int Foo {get;set;}
    }
    [DataContract]
    class Building : Node {
        [DataMember]
        public string Bar {get;set;}
    }
    
    static class Program
    {
        static void Main()
        {
            Dictionary<Guid, Node> data = new Dictionary<Guid, Node>();
            Type type = typeof(Dictionary<Guid, Node>);
            data.Add(Guid.NewGuid(), new Building { Foo = 1, Bar = "a" });
            StringWriter sw = new StringWriter();
            using (XmlWriter xw = XmlWriter.Create(sw))
            {
                DataContractSerializer dcs = new DataContractSerializer(type);
                dcs.WriteObject(xw, data);
            }
    
            string xml = sw.ToString();
    
            StringReader sr = new StringReader(xml);
            using (XmlReader xr = XmlReader.Create(sr))
            {
                DataContractSerializer dcs = new DataContractSerializer(type);
                Dictionary<Guid, Node> clone = (Dictionary<Guid, Node>)
                    dcs.ReadObject(xr);
                foreach (KeyValuePair<Guid, Node> pair in clone)
                {
                    Console.WriteLine(pair.Key + ": " + pair.Value.Foo + "/" +
                        ((Building)pair.Value).Bar);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 191k
  • Answers 191k
  • 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 There is no List Partitioning in SQL Server 2008. But… May 12, 2026 at 6:12 pm
  • Editorial Team
    Editorial Team added an answer Unfortunately, the only way to set the default value of… May 12, 2026 at 6:12 pm
  • Editorial Team
    Editorial Team added an answer you can use wget, a command line browser to accomplish… May 12, 2026 at 6:12 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.