这是我的源代码,它工作正常,但它有一个问题。它不会加载我在youtube上跟随的XML文件,因为这应该是一个项目。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace Addressbook_Csharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Person> people = new List<Person>();
private void Form1_Load(object sender, EventArgs e)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (!Directory.Exists(path + "\\Address Book - Group 1"))
Directory.CreateDirectory(path + "\\Address Book - Group 1");
if (!File.Exists(path + "\\Address Book - Group 1\\settings.xml"))
File.Create((path + "\\Address Book - Group 1\\settings.xml"));
{
XmlTextWriter xW = new XmlTextWriter(path + "\\Address Book - Group 1\\settings.xml", Encoding.UTF8);
xW.WriteStartElement("People");
xW.WriteEndElement();
xW.Close();
}
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path + "\\Address Book - Group 1\\settings.xml");
foreach (XmlNode xNode in xDoc.SelectSingleNode("People"))
{
Person p = new Person();
p.Name = xNode.SelectSingleNode("Name").InnerText;
p.Address = xNode.SelectSingleNode("Address").InnerText;
p.Contacts = xNode.SelectSingleNode("Name").InnerText;
p.Email = xNode.SelectSingleNode("Email").InnerText;
p.Birthday = DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("Birthday").InnerText));
people.Add(p);
listView1.Items.Add(p.Name);
}
}
private void button2_Click(object sender, EventArgs e)
{
Person p = new Person();
p.Name = textBox1.Text;
p.Address = textBox2.Text;
p.Email = textBox4.Text;
p.Birthday = dateTimePicker1.Value;
p.Contacts = textBox3.Text;
people.Add(p);
listView1.Items.Add(p.Name);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
dateTimePicker1.Value = DateTime.Now;
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0) return;
textBox1.Text = people[listView1.SelectedItems[0].Index].Name;
textBox2.Text = people[listView1.SelectedItems[0].Index].Address;
textBox3.Text = people[listView1.SelectedItems[0].Index].Contacts;
textBox4.Text = people[listView1.SelectedItems[0].Index].Email;
dateTimePicker1.Value = people[listView1.SelectedItems[0].Index].Birthday;
}
private void button3_Click(object sender, EventArgs e)
{
Remove();
}
void Remove()
{
try
{
listView1.Items.Remove(listView1.SelectedItems[0]);
people.RemoveAt(listView1.SelectedItems[0].Index);
}
catch { }
}
private void removeToolStripMenuItem_Click(object sender, EventArgs e)
{
Remove();
}
private void button1_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0) return;
people[listView1.SelectedItems[0].Index].Name = textBox1.Text;
people[listView1.SelectedItems[0].Index].Address = textBox2.Text;
people[listView1.SelectedItems[0].Index].Contacts = textBox3.Text;
people[listView1.SelectedItems[0].Index].Email = textBox4.Text;
people[listView1.SelectedItems[0].Index].Birthday = dateTimePicker1.Value;
listView1.SelectedItems[0].Text = textBox1.Text;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path + "\\Address Book - Group 1\\settings.xml");
XmlNode xNode = xDoc.SelectSingleNode("People");
xNode.RemoveAll();
foreach (Person p in people)
{
XmlNode xTop = xDoc.CreateElement("Person");
XmlNode xName = xDoc.CreateElement("Name");
XmlNode xAddress = xDoc.CreateElement("Address");
XmlNode xContacts = xDoc.CreateElement("Contacts");
XmlNode xEmail = xDoc.CreateElement("Email");
XmlNode xBirthday = xDoc.CreateElement("Birthday");
xName.InnerText = p.Name;
xAddress.InnerText = p.Address;
xContacts.InnerText = p.Contacts;
xEmail.InnerText = p.Email;
xBirthday.InnerText = p.Birthday.ToFileTime().ToString();
xTop.AppendChild(xName);
xTop.AppendChild(xAddress);
xTop.AppendChild(xContacts);
xTop.AppendChild(xEmail);
xTop.AppendChild(xBirthday);
xDoc.DocumentElement.AppendChild(xTop);
}
xDoc.Save(path + "\\Address Book - Group 1\\settings.xml");
}
class Person
{
public string Name
{
get;
set;
}
public string Address
{
get;
set;
}
public string Contacts
{
get;
set;
}
public string Email
{
get;
set;
}
public DateTime Birthday
{
get;
set;
}
}
}
}