Функции записи и чтения:
Код: Выделить всё
public static List ReadXML(string xmlfilepath)
{
try
{
List customers = new List();
XmlSerializer serializer = new XmlSerializer(typeof(List));
TextReader reader = new StringReader(xmlfilepath);
customers.AddRange((List)serializer.Deserialize(reader));
reader.Close();
reader = null;
serializer = null;
return customers;
}
catch (Exception)
{
throw;
}
}
public static bool WriteXML(List customers, string filePath)
{
try
{
FileIO.Delete(filePath);
XmlSerializer serializer = new XmlSerializer(typeof(List));
TextWriter writer = new StreamWriter(filePath);
serializer.Serialize(writer, customers);
writer.Close();
writer = null;
serializer = null;
return true;
}
catch (Exception)
{
throw;
}
}
Код: Выделить всё
private void frmBanking_Load(object sender, EventArgs e)
{
// Read XML file and use it to display customer information
try
{
settings = Program.Configuration.GetSection("MySettings").Get();
this.Text = settings.Text;
lblStatus.ForeColor = Color.Black;
// customers = CustomerManager.Populate();
customers = CustomerManager.ReadXML(settings.CustomerXMLFilename);
Refresh();
}
catch (Exception ex)
{
lblStatus.ForeColor = Color.Red;
lblStatus.Text = ex.Message;
}
}
Код: Выделить всё
private void btnWriteToFile_Click(object sender, EventArgs e)
{
// write customers and transactions to flat file - similar to WriteXMl function
try
{
lblStatus.ForeColor = Color.Black;
lblStatus.Text = string.Empty;
if (customers != null)
{
bool results = CustomerManager.WriteXML(customers, settings.CustomerXMLFilename);
}
}
catch (Exception ex)
{
lblStatus.ForeColor = Color.Red;
lblStatus.Text = ex.Message;
}
}
private void btnSave_Click(object sender, EventArgs e)
{
// updates the information in the existing objects - similar to ReadXML function
try
{
lblStatus.ForeColor = Color.Black;
lblStatus.Text = string.Empty;
customers = CustomerManager.ReadXML(settings.CustomerXMLFilename);
Refresh();
}
catch (Exception ex)
{
lblStatus.ForeColor = Color.Red;
lblStatus.Text = ex.Message;
}
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... anking-app
Мобильная версия