3

I am trying to convert an object say User having multiple fields into an XML. The problem is that I get an exception when it tries to serialize an ILIST member. Following is the code I am using:

var stringwriter = new System.IO.StringWriter(); 
var serializer = new XmlSerializer(User.GetType()); <!-- Getting exception here -->
serializer.Serialize(stringwriter, User); 
return stringwriter.ToString();

My User looks something like this:

class User
{       public virtual string Name{ get; set; }
        public virtual DateTime? LastUpdated { get; set; }

        public virtual int? ContactId { get; set; }

        public virtual IList<Sector> Sectors { get; set; }
        public virtual AccessLevel AccessLevel { get; set; }
        public virtual IList<UserRole> UserRole { get; set; }
}

Is there a way by which I can check if a member of the class whose object I am serializing is serializable or not and then tweak the logic for that particular member. I have less control over the user class. :( So any way where I could extend my code to handle IList members will be preferred.

Ross Cooper
  • 245
  • 2
  • 10
  • 20
  • try mark `User` class as `serializable`, for that add [SerializableAttribute](http://msdn.microsoft.com/en-us/library/system.serializableattribute(v=vs.110).aspx) – Grundy Jan 09 '14 at 13:18
  • 1
    What is the exception message? – Alberto Jan 09 '14 at 13:18
  • `User` in your code it and class and object or only class? – Grundy Jan 09 '14 at 13:22
  • Cannot serialize member 'User.Sectors' of type 'System.Collections.IList – Ross Cooper Jan 09 '14 at 13:22
  • While serializing User is an instance not class. – Ross Cooper Jan 09 '14 at 13:24
  • try see this [post](http://stackoverflow.com/questions/3632769/cannot-serialize-member-because-it-is-an-interface), so you simply should not use interface – Grundy Jan 09 '14 at 13:26
  • here is the same topic http://stackoverflow.com/questions/10012736/how-to-serialize-an-interface-such-as-ilistt – potehin143 Jan 09 '14 at 13:49
  • possible duplicate of [Serialize an Object to XML: IList Property Causes Exception](http://stackoverflow.com/questions/9879618/serialize-an-object-to-xml-ilistcustomobject-property-causes-exception) – nawfal Jul 10 '14 at 15:30

1 Answers1

6

The old style XML serialization can't handle interfaces - it wants concrete types.

However, you can do it with the newer DataContractSerializer.

Here's an example based on your code:

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

namespace Demo
{
    [DataContract(Namespace = "")]
    public class User
    {
        [DataMember]
        public virtual string Name
        {
            get;
            set;
        }

        [DataMember]
        public virtual DateTime? LastUpdated
        {
            get;
            set;
        }

        [DataMember]
        public virtual int? ContactId
        {
            get;
            set;
        }

        [DataMember]
        public virtual IList<string> Sectors
        {
            get;
            set;
        }
    }

    internal class Program
    {
        private void run()
        {
            User user = new User();

            user.Sectors = new[] {"One", "Two", "Three"};
            user.Name = "Test Name";

            // Serialize

            var result = new StringBuilder();
            DataContractSerializer serializer = new DataContractSerializer(user.GetType());

            using (var stringWriter = new StringWriter(result))
            using (var xmlWriter    = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true } ))
            {
                serializer.WriteObject(xmlWriter, user);
            }

            string serialisedToString = result.ToString();

            Console.WriteLine(serialisedToString);

            // Deserialize

            using (var stringReader = new StringReader(serialisedToString))
            using (var xmlReader    = XmlReader.Create(stringReader))
            {
                user = (User) serializer.ReadObject(xmlReader);
            }

            Console.WriteLine(user.Name);
        }

        private static void Main()
        {
            new Program().run();
        }
    }
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Is there a way by which I can check if a member of the class whose object I am serializing is serializable or not and then tweak the logic for that particular member. I have less control over the user class. :( So any way where I could extend my code to handle IList members will be preferred. – Ross Cooper Jan 10 '14 at 04:59