我正在制定一个计划来管理我们公司的所有客户。我想使用 XML 文件来保存客户。所以我创建了一个 XML 文件(如果不存在)并用元素填充它。在这里你可以看到我是怎么做的:
string path = "C:\\Users\\Römel\\Desktop\\Save";
List<Kunde> kunde = new List<Kunde>();
private void Form1_Load(object sender, EventArgs e)
{
XmlDocument xdocument = new XmlDocument();
xdocument.Load(path + "\\save.xml");
foreach (XmlNode xnode in xdocument.SelectNodes("Kundenverwaltung/Kunde"))
{
Kunde k = new Kunde();
k.KundenNr = xnode.SelectSingleNode("KundenNr").InnerText;
k.Nachname = xnode.SelectSingleNode("Nachname").InnerText;
k.Vorname = xnode.SelectSingleNode("Vorname").InnerText;
k.Adresse = xnode.SelectSingleNode("Adresse").InnerText;
k.Ort = xnode.SelectSingleNode("Ort").InnerText;
k.Telefon = xnode.SelectSingleNode("Telefon").InnerText;
k.Mail = xnode.SelectSingleNode("Mail").InnerText;
kunde.Add(k);
listViewKunden.Items.Add(k.Nachname + " " + k.Vorname);
}
}
private void btnAddCustomer_Click(object sender, EventArgs e)
{
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (!File.Exists(path + "\\save.xml"))
{
XmlTextWriter xwriter = new XmlTextWriter(path + "\\save.xml", Encoding.UTF8);
xwriter.WriteStartElement("Kundenverwaltung");
xwriter.WriteEndElement();
xwriter.Close();
}
Kunde k = new Kunde();
k.KundenNr = txtKundenNr.Text;
k.Nachname = txtKundeNachname.Text;
k.Vorname = txtKundeVorname.Text;
k.Adresse = txtKundeAdresse.Text;
k.Ort = txtKundeOrt.Text;
k.Telefon = txtKundeTel.Text;
k.Mail = txtKundeMail.Text;
kunde.Add(k);
listViewKunden.Items.Add(k.Nachname + " " + k.Vorname);
txtKundenNr.Text = "";
txtKundeNachname.Text = "";
txtKundeVorname.Text = "";
txtKundeAdresse.Text = "";
txtKundeOrt.Text = "";
txtKundeTel.Text = "";
txtKundeMail.Text = "";
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(path + "\\save.xml");
XmlNode xnode = xdoc.SelectSingleNode("Kundenverwaltung");
xnode.RemoveAll();
foreach (Kunde k in kunde)
{
XmlNode xtop = xdoc.CreateElement("Kunde");
XmlNode xkundennr = xdoc.CreateElement("KundenNr");
XmlNode xnachname = xdoc.CreateElement("Nachname");
XmlNode xvorname = xdoc.CreateElement("Vorname");
XmlNode xadresse = xdoc.CreateElement("Adresse");
XmlNode xort = xdoc.CreateElement("Ort");
XmlNode xtel = xdoc.CreateElement("Telefon");
XmlNode xmail = xdoc.CreateElement("Mail");
xkundennr.InnerText = k.KundenNr;
xnachname.InnerText = k.Vorname;
xvorname.InnerText = k.Nachname;
xadresse.InnerText = k.Adresse;
xort.InnerText = k.Ort;
xtel.InnerText = k.Telefon;
xmail.InnerText = k.Mail;
xtop.AppendChild(xkundennr);
xtop.AppendChild(xnachname);
xtop.AppendChild(xvorname);
xtop.AppendChild(xadresse);
xtop.AppendChild(xort);
xtop.AppendChild(xtel);
xtop.AppendChild(xmail);
xdoc.DocumentElement.AppendChild(xtop);
}
xdoc.Save(path + "\\save.xml");
e.Cancel = false;
this.Close();
}
对于KundenNr
等,Nachname
我在文件末尾创建了一些类。我希望这不是太多的代码。
第一:我不能使用右上角的常规“X”按钮,因为如果我点击它没有反应。所以我创建了一个Exit
按钮。
第二:每次我点击退出按钮离开程序时,都会在这一行出现错误:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
xdoc.Load(path + "\\save.xml");
}
“缺少根元素”出现。有人知道这个错误并且可以给我一个提示吗?
编辑:因为btnExit_Click
我使用此代码:
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}