Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XmlSerializer doesn't serialize properties when inheriting List<T>? [modified]

XmlSerializer doesn't serialize properties when inheriting List<T>? [modified]

Scheduled Pinned Locked Moved C#
csharpxmltutorialquestionannouncement
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mav northwind
    wrote on last edited by
    #1

    Hi folks! The XmlSerializer is giving me a headache. I've got this class (.NET 2.0):

    [Serializable]
    public class ParticipantList : List<Participant>
    {
    private Guid _ID = Guid.NewGuid();
    public Guid ID
    {
    get { return _ID; }
    set { _ID = value; }
    }

    public static void ToFile(ParticipantList list, string filename)
    {
    using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
    {
    XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
    xs.Serialize(fs, list);
    }
    }

    public static ParticipantList FromFile(string filename)
    {
    using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
    XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
    return (ParticipantList)xs.Deserialize(fs);
    }
    }
    }

    Pretty basic stuff it would seem. The participant list gets serialized OK, but the ID isn't included in the XML file! :( Did I miss something? Has the XmlSerializer gone nuts? Thanks in advance for tips on how to get the ID serialized as well! [Update] No additional property or member gets serialized, the Guid isn't the reason.

    Regards, mav -- Black holes are the places where God divided by 0...

    modified on Wednesday, February 17, 2010 6:16 AM

    L M 2 Replies Last reply
    0
    • M mav northwind

      Hi folks! The XmlSerializer is giving me a headache. I've got this class (.NET 2.0):

      [Serializable]
      public class ParticipantList : List<Participant>
      {
      private Guid _ID = Guid.NewGuid();
      public Guid ID
      {
      get { return _ID; }
      set { _ID = value; }
      }

      public static void ToFile(ParticipantList list, string filename)
      {
      using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
      {
      XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
      xs.Serialize(fs, list);
      }
      }

      public static ParticipantList FromFile(string filename)
      {
      using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
      {
      XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
      return (ParticipantList)xs.Deserialize(fs);
      }
      }
      }

      Pretty basic stuff it would seem. The participant list gets serialized OK, but the ID isn't included in the XML file! :( Did I miss something? Has the XmlSerializer gone nuts? Thanks in advance for tips on how to get the ID serialized as well! [Update] No additional property or member gets serialized, the Guid isn't the reason.

      Regards, mav -- Black holes are the places where God divided by 0...

      modified on Wednesday, February 17, 2010 6:16 AM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It works for me, here is my test code :

      using System;
      using System.IO;
      using System.Xml.Serialization;

      internal class Program
      {
      private static void Main()
      {
      const string FILENAME = @"d:\test123.xml";

      MyClass myClassBeforeSerializing = new MyClass();
      
      XmlSerializer xmlSerializer = new XmlSerializer(typeof (MyClass));
      using (FileStream fs = new FileStream(FILENAME, FileMode.Create))
      {
        xmlSerializer.Serialize(fs, myClassBeforeSerializing);
      }
      
      MyClass myClassAfterSerializing = new MyClass();
      using (FileStream fs = new FileStream(FILENAME, FileMode.Open))
      {
        myClassAfterSerializing = (MyClass) xmlSerializer.Deserialize(fs);
      }
      
      Console.WriteLine(myClassBeforeSerializing.MyGuid.ToString());
      Console.WriteLine(myClassAfterSerializing.MyGuid.ToString());
      

      }
      }

      [Serializable]
      public class MyClass
      {
      private Guid _myGuid = Guid.NewGuid();

      public Guid MyGuid
      {
      get { return _myGuid; }
      set { _myGuid = value; }
      }
      }

      M 1 Reply Last reply
      0
      • L Lost User

        It works for me, here is my test code :

        using System;
        using System.IO;
        using System.Xml.Serialization;

        internal class Program
        {
        private static void Main()
        {
        const string FILENAME = @"d:\test123.xml";

        MyClass myClassBeforeSerializing = new MyClass();
        
        XmlSerializer xmlSerializer = new XmlSerializer(typeof (MyClass));
        using (FileStream fs = new FileStream(FILENAME, FileMode.Create))
        {
          xmlSerializer.Serialize(fs, myClassBeforeSerializing);
        }
        
        MyClass myClassAfterSerializing = new MyClass();
        using (FileStream fs = new FileStream(FILENAME, FileMode.Open))
        {
          myClassAfterSerializing = (MyClass) xmlSerializer.Deserialize(fs);
        }
        
        Console.WriteLine(myClassBeforeSerializing.MyGuid.ToString());
        Console.WriteLine(myClassAfterSerializing.MyGuid.ToString());
        

        }
        }

        [Serializable]
        public class MyClass
        {
        private Guid _myGuid = Guid.NewGuid();

        public Guid MyGuid
        {
        get { return _myGuid; }
        set { _myGuid = value; }
        }
        }

        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #3

        Hi! Thanks for your reply. Meanwhile I've found that the problem doesn't stem from the property being a Guid but from deriving from List<T>. No matter which additional properties I define, none of them gets serialized!

        Regards, mav -- Black holes are the places where God divided by 0...

        1 Reply Last reply
        0
        • M mav northwind

          Hi folks! The XmlSerializer is giving me a headache. I've got this class (.NET 2.0):

          [Serializable]
          public class ParticipantList : List<Participant>
          {
          private Guid _ID = Guid.NewGuid();
          public Guid ID
          {
          get { return _ID; }
          set { _ID = value; }
          }

          public static void ToFile(ParticipantList list, string filename)
          {
          using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
          {
          XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
          xs.Serialize(fs, list);
          }
          }

          public static ParticipantList FromFile(string filename)
          {
          using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
          {
          XmlSerializer xs = new XmlSerializer(typeof(ParticipantList));
          return (ParticipantList)xs.Deserialize(fs);
          }
          }
          }

          Pretty basic stuff it would seem. The participant list gets serialized OK, but the ID isn't included in the XML file! :( Did I miss something? Has the XmlSerializer gone nuts? Thanks in advance for tips on how to get the ID serialized as well! [Update] No additional property or member gets serialized, the Guid isn't the reason.

          Regards, mav -- Black holes are the places where God divided by 0...

          modified on Wednesday, February 17, 2010 6:16 AM

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          Ok, I've found a solution myself: According to this thread[^] the behaviour is "by design" and unsually no attributes of such a class are serialized. I've solved the problem by not deriving from List<T> but by including the list as a member. Thanks anyway,

          Regards, mav -- Black holes are the places where God divided by 0...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups