Buongiorno,
ho questo problema: salvo correttamente la lista Parts sul file con SerializeObject (ho controllato l'xml)
Quando però rileggo il file con DeserializeObject la Parts restituita ha un elemento nella lista Split i più.
Perchè?
Grazie.
Andrea
private List<Part> Parts = new List<Part>();
public struct Split
{
public int lowNote;
public int highNote;
public int transpose;
}
public class Part
{
public string Name { get; set; }
public int ChannelIn { get; set; }
public int Out { get; set; }
public int ChannelOut { get; set; }
public int Banco_0 { get; set; }
public int Banco_32 { get; set; }
public int PrgChange { get; set; }
public List<Split> Split { get; set; } = new List<Split>();
public Part()
{
Split split = new Split();
Split.Add(split);
}
}
//-------------------------------------------------------------------------------------------------------------
/// <summary> Serializza in XML </summary>
//-------------------------------------------------------------------------------------------------------------
private void SerializeObject(string filename)
{
//Console.WriteLine("Writing With XmlTextWriter");
XmlSerializer serializer = new XmlSerializer(typeof(List<Part>)); //Definisce il tipo da deserializzare
// Create an XmlTextWriter using a FileStream.
Stream fs = new FileStream(filename, FileMode.Create);
XmlTextWriter writer = new XmlTextWriter(fs, Encoding.Unicode);
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, Parts); //Serializza l'oggetto del tipo di cui sopra
writer.Close();
}
//-------------------------------------------------------------------------------------------------------------
/// <summary> DeSerializza da XML </summary>
//-------------------------------------------------------------------------------------------------------------
private void DeserializeObject(string filename)
{
// Create an instance of the XmlSerializer.
XmlSerializer serializer = new XmlSerializer(typeof(List<Part>));
// Declare an object variable of the type to be deserialized.
//List<Part> Parts;
using (Stream reader = new FileStream(filename, FileMode.Open))
{
// Call the Deserialize method to restore the object's state.
Parts = (List<Part>)serializer.Deserialize(reader);
nudNumPart.Maximum = Parts.Count;
nudNumPart_ValueChanged(null, null);
}
}