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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:54:10+00:00 2026-05-25T19:54:10+00:00

I have the following code in which I’m trying to serialize a list to

  • 0

I have the following code in which I’m trying to serialize a list to a file

public static void Serialize<T>(this IList<T> list, string fileName)  
{
   try
   {
      var ds = new DataContractSerializer(typeof(T));
      using (Stream s = File.Create(fileName))
          ds.WriteObject(s, list);  
   }
   catch (Exception e)
   {
      _logger.Error(e);
      throw;
   } 
} 

and I’m getting the exception:

Type ‘System.Collections.Generic.List`1[[MyClass,
MyNameSpace, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]]’ with data contract name
‘ArrayOf*MyClass*:http://schemas.datacontract.org/2004/07/MyNameSpace‘
is not expected. Consider using a DataContractResolver or add any
types not known statically to the list of known types – for example,
by using the KnownTypeAttribute attribute or by adding them to the
list of known types passed to DataContractSerializer.

[KnownType(typeof(MyClass))]
[DataContract]
public class MyClass
{
    #region Properties
    [DataMember]
    public string Foo{ set; get; }
    [DataMember]
    public string Bar{ set; get; }
}

Any ideas?

There is no inheritance.

  • 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-25T19:54:11+00:00Added an answer on May 25, 2026 at 7:54 pm

    What has happened is that the serializer is expecting an instance of T, not a list of T, so you want your method to be like so:

    public static void Serialize<T>(this IList<T> list, string fileName)  
    {
       try
       {
          var ds = new DataContractSerializer(list.GetType());
          using (Stream s = File.Create(fileName))
            ds.WriteObject(s, list);  
       }
       catch (Exception e)
       {
         _logger.Error(e);
         throw;
       } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following code which walks through an Excel file and this works
I'm trying to use opengl in C#. I have following code which fails with
I have the following code which reads in the follow file, append a \r\n
I have the following code which searches a text file on my phone's sd
I have the following code which lets me execute a workflow. This could be
I have the following code which I am are currently using .... Basically, this
I have the following code which I use to map a nested list in
I have the following code which is trying to stop propagation, works every where
I have the following code which works great:- $('.ajax a').click(function() { getpageajax(this.href); return false;
I have the following code which will convert a string to TitleCase. I would

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.