I'm fairly new to C# development, currently building a booking application for a marina.
(Have searched previous questions but haven't been successful in locating specifically what I'm seeking)
Use case:
- Enter client info via windows form interface,
- Store data as XML file - on button click (one per customer)
- Return data to gridView when searched by name via form. (e.g client lookup)
I have configured the program to create an XML file for the form input, However, I can not figure out how to create a separate XML file for each entry.
Currently, each time I enter the form data, it overwrites the previous XML data in the file.
Any solutions as to how to create separate/additional XML files as per above are much appreciated.
First time posting here, so apologies if I've omitted any necessary info.
Code is as follows:
// Save XML.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;
namespace Popeye_Booking_application
{
public class SaveXml
{
public static void SaveData(object obj, string filename)
{
XmlSerializer sr = new XmlSerializer(obj.GetType());
TextWriter writer = new StreamWriter(filename);
sr.Serialize(writer, obj);
writer.Close();
}
}
}
// Information.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Popeye_Booking_application
{
public class Information
{
private string data1;
private string data2;
private string data3;
private string data4;
private string data5;
private string data6;
private string data7;
public string Data1
{
get { return data1; }
set { data1 = value; }
}
public string Data2
{
get { return data2; }
set { data2 = value; }
}
public string Data3
{
get { return data3; }
set { data3 = value; }
}
public string Data4
{
get { return data4; }
set { data4 = value; }
}
public string Data5
{
get { return data5; }
set { data5 = value; }
}
public string Data6
{
get { return data6; }
set { data6 = value; }
}
public string Data7
{
get { return data7; }
set { data7 = value; }
}
}
}
// Form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Popeye_Booking_application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCreate_Click(object sender, EventArgs e)
{
try
{
Information info = new Information();
info.Data1 = textBoxData1.Text;
info.Data2 = textBoxData2.Text;
info.Data3 = textBoxData3.Text;
info.Data4 = textBoxData4.Text;
info.Data5 = textBoxData5.Text;
info.Data6 = textBoxData6.Text;
info.Data7 = textBoxData7.Text;
SaveXml.SaveData(info, "data.xml");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void label10_Click(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
Thanks in advance,
Informationjust use it as name of your XML file ;).