I created this class where it'll append to the XML called Email Alerts h1, body and ids. However, im having trouble running this file from the start (when i call LoadFromFile), when the file does not exist - so we'll have to create a file and start logging it.
public class EMailAlert
{
public string h1 { get; set; }
public string body { get; set; }
public string idList { get; set; }
public void Save(string fileName)
{
using (var stream = new FileStream(fileName, FileMode.Create))
{
var XML = new XmlSerializer(typeof(EMailAlert));
XML.Serialize(stream, this);
}
}
public static EMailAlert LoadFromFile(string fileName)
{
if (!File.Exists(fileName))
{
var file = new FileInfo(fileName);
file.Directory.Create();
var xmlFile = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Email Logger"));
xmlFile.Add(new XElement("EmailAlerts"));
xmlFile.Save(fileName);
}
using (var stream = new FileStream(fileName, FileMode.Open))
{
var XML = new XmlSerializer(typeof(EMailAlert));
return (EMailAlert)XML.Deserialize(stream);
}
}
}
When i run this code (there are no xml file - so it creates an XML file and then it throws this error {"<EmailAlerts xmlns=''> was not expected."}
Here is how the xml file looks like
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Email Logger-->
<EmailAlerts />
Not sure why its not sending back an empty EmailAlert when i call LoadFromFile.