I need to serialize a list of the following class.
That's just a part of the class that I'm using.
Anyways, I'm using the following to serialize it.
And then I'm using the following for testing
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?
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; }
}
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();
}
}
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);
}
"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?
Comment