How do I serialize the following class.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tyler Wiebe
    New Member
    • Mar 2011
    • 66

    How do I serialize the following class.

    I need to serialize a list of the following class.
    Code:
        public class Track
        {
            internal Track() { }
            public Track(string Artist, string Album, string Title)
            {
                this.Artist = Artist;
                this.Album = Album;
                this.Title = Title;
            }
    
            public string Artist { get; internal set; }
            public string Album { get; internal set; }
            public string Title { get; internal set; }
        }
    That's just a part of the class that I'm using.
    Anyways, I'm using the following to serialize it.
    Code:
            public void Save(string Location, string FileName, System.Collections.Generic.List<Track> TrackList)
            {
                if (System.IO.Directory.Exists(Location))
                {
                    System.Xml.Serialization.XmlSerializer Serializer = new System.Xml.Serialization.XmlSerializer(typeof(System.Collections.Generic.List<Track>));
                    System.IO.TextWriter TextWriter = new System.IO.StreamWriter(Location + FileName);
                    Serializer.Serialize(TextWriter, TrackList);
                    TextWriter.Close();
                }
            }
    And then I'm using the following for testing
    Code:
            public Form1()
            {
                InitializeComponent();
                System.Collections.Generic.List<Track> TrackList = new List<Track>();
                Track T1 = new Track("Hello 1", "Goodbye 1", "See You Again 1");
                Track T2 = new Track("Hello 2", "Goodbye 2", "See You Again 2");
                Track T3 = new Track("Hello 3", "Goodbye 3", "See You Again 3");
                Track T4 = new Track("Hello 4", "Goodbye 4", "See You Again 4");
                Track T5 = new Track();
                T5.Artist = "Hello 5";
                T5.Album = "Goodbye 5";
                T5.Title = "See You Again 5";
                Track T6 = new Track();
                T6.Artist = "Hello 6";
                T6.Album = "Goodbye 6";
                T6.Title = "See You Again 6";
                Track T7 = new Track();
                T7.Artist = "Hello 7";
                T7.Album = "Goodbye 7";
                T7.Title = "See You Again 7";
                Track T8 = new Track();
                T8.Artist = "Hello 8";
                T8.Album = "Goodbye 8";
                T8.Title = "See You Again 8";
    
                TrackList.Add(T1);
                TrackList.Add(T2);
                TrackList.Add(T3);
                TrackList.Add(T4);
                TrackList.Add(T5);
                TrackList.Add(T6);
                TrackList.Add(T7);
                TrackList.Add(T8);
    
                this.Save("C:\\", "Bob.xml", TrackList);
            }
    And I get the following exception:
    "Unable to generate a temporary class (result=1).
    error CS0200: Property or indexer 'SerializerTest .Track.Artist' cannot be assigned to -- it is read only
    error CS0200: Property or indexer 'SerializerTest .Track.Album' cannot be assigned to -- it is read only
    error CS0200: Property or indexer 'SerializerTest .Track.Title' cannot be assigned to -- it is read only"

    How do I get around this without changing the set property from internal?
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I don't know if you can get around it directly... the XmlSerializer uses the public properties to build your objects. You also have to have a parameterless constructor for similar reasons.

    That said, you might be able to implement the IXmlSerializabl e class. This is a less generic approach, giving you control over how things get written/read.

    This might work out for you as is, but if you still have trouble with that internal set property, I'd recommend you switch from auto-implement properties and use a member variable. You can still have an internal set, just return the member variable instead. Then your ReadXml method can set the member variable directly.

    I've never used this method before myself, I've just heard about it as an alternative to the XML serialization that you're done (which is what I'm used to using as well). Give it a try and let us know how it works out for you :)

    Comment

    • Tyler Wiebe
      New Member
      • Mar 2011
      • 66

      #3
      Well it's been a few days since I asked this question, but the day after I did, I thought of my solution, which from what I'm aware was something like the IXmlSerializabl e class. I just made a function that used a stream reader/writer to save to, and read from, a file, and it works fine, so thanks for your help anyway.

      Comment

      Working...