Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2007

Tip: Looking for answers? Try searching our database.

XMLSerialize a class with members / properties that are interfaces

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fabian - 07 Dec 2007 15:18 GMT
I want to serialize a large tree structure of different objects to a xml
file. To get a loose coupling design some classes have interface members. At
runtime I get an InvalidOperationException: Cannot serialize member XYZ
because it is an interface.

To boil the thing down to a test example:

   public interface IProblematicMember
   {
   }

   public class ProblematicMember: IProblematicMember
   {

   }

   public class DoesNotSerialize
   {
       private IProblematicMember problematicMember;

       public IProblematicMember ProblematicMember
       {
           get { return problematicMember; }
           set { problematicMember = value; }
       }
   }

   class Program
   {
       static void Main(string[] args)
       {
           DoesNotSerialize dns = new DoesNotSerialize();
           XmlSerializer mySerializer = new
XmlSerializer(typeof(DoesNotSerialize));
           StreamWriter myWriter = new StreamWriter("myFileName.xml");
           mySerializer.Serialize(myWriter, dns);
           myWriter.Close();
       }
   }

Is there any possibility to solve this, e.g. by implementing the
serialization by hand?

Thanks for your help,

Fabian
Frans Bouma [C# MVP] - 08 Dec 2007 10:25 GMT
> I want to serialize a large tree structure of different objects to a
> xml file. To get a loose coupling design some classes have interface
[quoted text clipped - 38 lines]
> Is there any possibility to solve this, e.g. by implementing the
> serialization by hand?

    You have to implement IXmlSerializable and indeed do the serialization
by hand.

        FB

Signature

------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.