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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:12:20+00:00 2026-05-16T03:12:20+00:00

I’m trying to serialize a class derived from List<> using DataContract. The problem is

  • 0

I’m trying to serialize a class derived from List<> using DataContract. The problem is that properties of my class won’t get serialized.

My classes:

[CollectionDataContract] /*[Serializable]*/ /*[DataContract]*/
public class DownloadRuleCollection : List<DownloadRule> {

    [DataMember(EmitDefaultValue = false)]
    public string SomeProperty { get; set; }
    //this is, in fact, more complex, but this is enough for the example
}

[DataContract]
public class DownloadRule {
    [DataMember(EmitDefaultValue = false)]
    public string Name { get; set; }

    /*
     more properties
     ...
     */
}

Test:

static void Main(string[] args) {

    //fill test collection with some data...
    var col = new DownloadRuleCollection { SomeProperty = "someText" };

    var rule = new DownloadRule { Name = "test01" };
    col.Add(rule);

    rule = new DownloadRule { Name = "test02" };
    col.Add(rule);

    rule = new DownloadRule { Name = "test03" };
    col.Add(rule);

    //serialize
    Console.WriteLine("serializing");

    Serialize(col, "serializationTest.xml");

    Console.WriteLine("serialized");
    Console.ReadLine();
}

result:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDownloadRule xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication1">
  <DownloadRule>
    <Name>test01</Name>
  </DownloadRule>
  <DownloadRule>
    <Name>test02</Name>
  </DownloadRule>
  <DownloadRule>
    <Name>test03</Name>
  </DownloadRule>
</ArrayOfDownloadRule>

As you can see, the List’s items are serialized (and deserialized) properly, but the List itself does not get serialized. I have tried to use different attributes:
[Serializable], no change;
[DataContract], throws exception during serialization (collections cant use this attribute)

btw I am serializing also private fields, so I cannot use XmlSerializer (or other classes that can’t serialize private fields).

  • 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-16T03:12:21+00:00Added an answer on May 16, 2026 at 3:12 am

    Ok, so Climber104’s solution would work, but I would need to re-implement all of the List‘s methods, which makes me feel that I am reinventing the wheel.

    JaredPar from Jarek Waliszko’s thread suggests to use a wrapper class.
    The easiest is to use it just for the sake of serialization process, so I used an protected innner wrapper class. This allows me to achieve my goals with just a few lines of code needed.

    public class DownloadRuleCollection : List<DownloadRule> {
    
        public string SomeProperty { get; set; }
    
        public void Serialize(string fileName) {
            Serializer.Serialize(
                new DownloadRuleCollection_SerializationWrapper {
                    Collection = this,
                    SomeProperty = SomeProperty
                }, fileName);
        }
    
        public static DownloadRuleCollection Deserialize(string fileName) {
            var wrapper = Serializer.Deserialize<DownloadRuleCollection_SerializationWrapper>(fileName);
    
            var result = wrapper.Collection;
            result.SomeProperty = wrapper.SomeProperty;
    
            return result;
        }
    
        [DataContract(Name = "DownloadRuleCollection")]
        private class DownloadRuleCollection_SerializationWrapper {
    
            [DataMember(EmitDefaultValue = false, Name = "SomeProperty", Order = 0)]
            public string SomeProperty { get; set; }
    
            [DataMember(EmitDefaultValue = false, Name = "DownloadRules", Order = 1)]
            public DownloadRuleCollection Collection;
    
        }
    }
    
    [DataContract]
    public class DownloadRule {
        [DataMember(EmitDefaultValue = false)]
        public string Name { get; set; }
    }
    
    public static class Serializer {
        public static void Serialize<T>(T obj, string fileName) {
            using(XmlWriter writer = XmlWriter.Create(fileName, new XmlWriterSettings { Indent = true }))
                new DataContractSerializer(typeof(T)).WriteObject(writer, obj);
        }
    
        public static T Deserialize<T>(Stream stream) {
            return (T)new DataContractSerializer(typeof(T)).ReadObject(stream);
        }
    
        public static T Deserialize<T>(string fileName) {
            using(FileStream fs = File.OpenRead(fileName)) {
                return Deserialize<T>(fs);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.