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

The Archive Base Latest Questions

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

I am converting a project so I can use Moq (Mock) testing. I have

  • 0

I am converting a project so I can use Moq (Mock) testing.

I have a class which has a List of interfaces. I understand interfaces cannot be serialized. So I am trying to work around it.

I have created a console to test, however, after trying numerous methods from other SO posts ( Serializing a List hold an interface to XML, XmlSerializer serialize generic List of interface); I feel I need to ask for help.

The sample code below wraps the List in its own class so as to be able to implement ISerializable interface. However, I have the following summarized error:

Cannot serialize member MyConcreteCollection.ChildList of type IChild because 
it is an interface.

The issue is in this code

public void WriteXml(System.Xml.XmlWriter writer)
{
    //-- HAVE also tried IChild child in ChildList
    foreach (ConcreteChild child in ChildList)
    {
        XmlSerializer s = new XmlSerializer(child .GetType());
        s.Serialize(writer, child );
    }
}

Here is the full code list. This is working and has fix implemented by user Jay

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

[Serializable]
public class ConcreteParent
{
    public MyConcreteCollection MyConcreteCollection { get; set; }
}

[Serializable]
public class MyConcreteCollection : IXmlSerializable //(Was ISerializable, as fixed by user Jay)
{
    public List<IChild> ChildList { get; set; }

    public System.Xml.Schema.XmlSchema GetSchema() { return null; }
    public void ReadXml(System.Xml.XmlReader reader) { }
    public void GetObjectData(SerializationInfo info, StreamingContext context) { }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        foreach (ConcreteChild child in ChildList)
        {
            XmlSerializer s = new XmlSerializer(child.GetType());
            s.Serialize(writer, child);
        }
    }

}

public interface IChild { }
[Serializable] public class ConcreteChild : IChild { }


public static class SerializerHelper<T>
{
    public static string Serialize(T myobject)
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        StringWriter stringWriter = new StringWriter();
        xmlSerializer.Serialize(stringWriter, myobject);
        string xml = stringWriter.ToString();

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml);
        StringWriter sw = new StringWriter();
        XmlTextWriter xw = new XmlTextWriter(sw);
        xmlDoc.WriteTo(xw);

        return sw.ToString();
    }
}

class Program
{
    static void Main(string[] args)
    {
        try
        {
            ConcreteParent concreteParent = new ConcreteParent();
            concreteParent.MyConcreteCollection = new MyConcreteCollection();
            concreteParent.MyConcreteCollection.ChildList = new List<IChild>();
            concreteParent.MyConcreteCollection.ChildList.Add(new ConcreteChild());

            string xml = SerializerHelper<ConcreteParent>.Serialize(concreteParent);
            Console.WriteLine(xml);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        Console.ReadKey();
    }
}
  • 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-25T09:33:49+00:00Added an answer on May 25, 2026 at 9:33 am

    You have MyConcreteCollection implementing ISerializable. As shown in the first post to which you linked, you need instead to implement IXmlSerializable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When converting a project (in which a template method of IComparable was used a
I am converting my project to use DLLs and am trying to break apart
I'm converting a project to use ObjectDataSources instead of SqlDataSources. I'm using text boxes
I am converting from vb5 and have a dll (now converted to VB.NET) which
I have a java project in which I have to save the data of
I am considering converting a project that I've inherited from .net 1.1 to .net
I'm converting a project from vs2008 to vs2010 and getting linker errors for std:ifstream/ofstream
Since I'm more comfortable using Eclipse, I thought I'd try converting my project from
My project is converting a legacy fat-client desktop application into the web. The database
I am converting a makefile project into a Visual Studio VC++ project. It's actually

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.