i've read about it and it looks pretty easy, but i don't know why it crashes everytime on the XmlSerializer Line, i'd like to know why since people answered questions for convertion of list into xml with something similar and it doesn't seem to work for me!
Here is the code :
Class :
[Serializable()]
public class Pogos
{
public string Pogo { get; set; }
public DateTime Date { get; set; }
}
List for the daily pogos :
public static List<Pogos> DailyPogos = null;
Setting the Daily Pogo list to what is in the .xml
public static void SetDailyPogos()
{
string path = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/DailyPogos.xml");
if (File.Exists(path))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Pogos>));
StreamReader reader = new StreamReader(path);
DailyPogos = (List<Pogos>)serializer.Deserialize(reader);
reader.Close();
foreach (var pogo in DailyPogos)
{
if (pogo.Date.Date != DateTime.Now.Date)
DailyPogos.Remove(pogo);
}
}
else
DailyPogos = new List<Pogos>();
}
Save the Daily Pogos (distinct) in the .xml
public static void SaveDailyPogos(List<string> pogos)
{
foreach (var pogo in pogos)
{
var matches = DailyPogos.Where(e => e.Pogo.Contains(pogo));
foreach ( var match in matches)
{
DailyPogos.Remove(match);
}
var pog = new Pogos();
pog.Pogo = pogo;
pog.Date = DateTime.Now;
DailyPogos.Add(pog);
}
string path = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/DailyPogos.xml");
//create the serialiser to create the xml
XmlSerializer serialiser = new XmlSerializer(typeof(List<Pogos>));
// Create the TextWriter for the serialiser to use
TextWriter filestream = new StreamWriter(path);
//write to the file
serialiser.Serialize(filestream, DailyPogos);
// Close the file
filestream.Close();
}
The line causing trouble ( no errors just crash ) :
XmlSerializer serializer = new XmlSerializer(typeof(List<Pogos>));
EDIT : I was pretty dumb in this situation, first, i could of watch the exception by adding a try catch which i don't know why i didn't.. Was probably tired. Exception was saying that it was failing because of the protection level of the application, which got fixed by adding "public" to the program class.