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. Serialize subclass of windows.forms.label

Serialize subclass of windows.forms.label

Scheduled Pinned Locked Moved C#
xmlquestionlearning
2 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.
  • D Offline
    D Offline
    DanielSheets
    wrote on last edited by
    #1

    I have a subclass of Windows.Forms.Label that I'd like to serialize to XML without using IXmlSerializable. A windows control (a Label in this case) isn't serializable. Consider the following...

    public class SubLabel : Windows.Forms.Label
    {
    public Text {get; set;}
    public int LocationX {get; set;}
    public int LocationY {get; set;}

    public void Save(string path)
    {
    XmlSerializer xs = new XmlSerializer(typeof(SubLabel));
    using (StreamWriter sw = new StreamWriter(path))
    {
    xs.Serialize(sw, this);
    }
    }

    public static SubLabel Load(string path)
    {
    XmlSerializer xs = new XmlSerializer(typeof(TestClass));
    using (StreamReader sr = new StreamReader(path))
    {
    return (SubLabel)xs.Deserialize(sr);
    }
    }
    ...
    }

    All I want to serialize are the public properties above. I'd like to keep it simple by using the Save() and Load() methods above. Of course this fails because of the Label parent class. Is there an easy way to do this?

    R 1 Reply Last reply
    0
    • D DanielSheets

      I have a subclass of Windows.Forms.Label that I'd like to serialize to XML without using IXmlSerializable. A windows control (a Label in this case) isn't serializable. Consider the following...

      public class SubLabel : Windows.Forms.Label
      {
      public Text {get; set;}
      public int LocationX {get; set;}
      public int LocationY {get; set;}

      public void Save(string path)
      {
      XmlSerializer xs = new XmlSerializer(typeof(SubLabel));
      using (StreamWriter sw = new StreamWriter(path))
      {
      xs.Serialize(sw, this);
      }
      }

      public static SubLabel Load(string path)
      {
      XmlSerializer xs = new XmlSerializer(typeof(TestClass));
      using (StreamReader sr = new StreamReader(path))
      {
      return (SubLabel)xs.Deserialize(sr);
      }
      }
      ...
      }

      All I want to serialize are the public properties above. I'd like to keep it simple by using the Save() and Load() methods above. Of course this fails because of the Label parent class. Is there an easy way to do this?

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      You could copy the selected public properties (Text, LocationX, LocationY) to a separate class and just serialize that.  Your Load() method would then deserialize that class and return a new SubLabel with those properties set. /ravi

      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

      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