public class Contact
{
public int Id { get ; set ; }
public string FirstName { get ; set ; }
public string LastName { get ; set ; }
public string Company { get ; set ; }
public string City { get ; set ; }
public string Telephone { get ; set ; }
public string Fax { get ; set ; }
public string Email { get ; set ; }
}
using System.Collections .Generic
using System.IO
using System.Linq
using System.Xml .Linq
namespace LinqXmlContactsBLL
{
public class LinqXmlContactsBLManager
{
public List<Contact> GetAllContacts()
{
XDocument xmlDocument = XDocument.Load ("Contacts.xml" )
IEnumerable<XElement> xmlContacts = xmlDocument.Element ("Contacts" )
.Elements ("Contact" )
List<Contact> contacts = new List<Contact>()
//获取联系人列表
foreach (XElement i in xmlContacts)
{
Contact contact = new Contact()
contact.Id = int.Parse (i.Attribute ("Id" ).Value )
contact.FirstName = i.Element ("FirstName" ).Value
contact.LastName = i.Element ("LastName" ).Value
contact.Company = i.Element ("Company" ).Value
contact.City = i.Element ("City" ).Value
contact.Telephone = i.Element ("Telephone" ).Value
contact.Fax = i.Element ("Fax" ).Value
contact.Email = i.Element ("Email" ).Value
contacts.Add (contact)
}
return contacts
}
public Contact GetContactById(int id)
{
XDocument xmlDocument = XDocument.Load ("Contacts.xml" )
XElement xmlContact = xmlDocument.Element ("Contacts" ).Elements ("Contact" )
.Where (a => a.Attribute ("Id" ).Value == id.ToString ()).FirstOrDefault ()
Contact contact = new Contact()
contact.Id = int.Parse (xmlContact.Attribute ("Id" ).Value )
contact.FirstName = xmlContact.Element ("FirstName" ).Value
contact.LastName = xmlContact.Element ("LastName" ).Value
contact.Company = xmlContact.Element ("Company" ).Value
contact.City = xmlContact.Element ("City" ).Value
contact.Telephone = xmlContact.Element ("Telephone" ).Value
contact.Fax = xmlContact.Element ("Fax" ).Value
contact.Email = xmlContact.Element ("Email" ).Value
return contact
}
public void AddContact(Contact contact)
{
XDocument xmlDocument = XDocument.Load ("Contacts.xml" )
//将值插入联系人xml文档
xmlDocument.Element ("Contacts" ).Add (
new XElement("Contact" ,
new XAttribute("Id" , contact.Id ),
new XElement("FirstName" , contact.FirstName ),
new XElement("LastName" , contact.LastName ),
new XElement("Company" , contact.Company ),
new XElement("City" , contact.City ),
new XElement("Telephone" , contact.Telephone ),
new XElement("Fax" , contact.Fax ),
new XElement("Email" , contact.Email )
))
xmlDocument.Save ("Contacts.xml" )
}
public void UpdateContact(Contact contact)
{
XDocument xmlDocument = XDocument.Load ("Contacts.xml" )
//通过ID更新联系人
XElement xmlContact = xmlDocument.Element ("Contacts" ).Elements ("Contact" )
.Where (x => x .Attribute ("Id" ).Value == contact.Id .ToString ())
.FirstOrDefault ()
//更新联系人
xmlContact.SetElementValue ("FirstName" , contact.FirstName )
xmlContact.SetElementValue ("LastName" , contact.LastName )
xmlContact.SetElementValue ("Company" , contact.Company )
xmlContact.SetElementValue ("City" , contact.City )
xmlContact.SetElementValue ("Telephone" , contact.Telephone )
xmlContact.SetElementValue ("Fax" , contact.Fax )
xmlContact.SetElementValue ("Email" , contact.Email )
xmlDocument.Save ("Contacts.xml" )
}
public void DeleteContact(int id)
{
XDocument xmlDocument = XDocument.Load ("Contacts.xml" )
//得到联系人并删除他
xmlDocument.Root .Elements ().Where (x => x .Attribute ("Id" ).Value == id.ToString ())
.FirstOrDefault ().Remove ()
xmlDocument.Save ("Contacts.xml" )
}
public int GetNextId()
{
int currentId
XDocument xmlDocument = XDocument.Load ("Contacts.xml" )
//获取id的集合
IEnumerable<int> contactIds = xmlDocument.Element ("Contacts" ).Elements ("Contact" )
.Select (x => int.Parse (x .Attribute ("Id" ).Value ))
//如果集合为空,则将变量设置为0 ,否则将该变量设置为最高ID值
if (contactIds.Count () == 0 )
currentId = 0
else
currentId = contactIds.Max ()
return currentId + 1
}
public void SetXmlFile()
{
//如果xml文件丢失,则使用根元素创建它
if (!(File.Exists ("Contacts.xml" )))
{
XDocument xmlDocument = new XDocument(
new XDeclaration("1.0" , "utf-8" , "yes" ),
new XElement("Contacts" ))
xmlDocument.Save ("Contacts.xml" )
}
}
public bool IdExists(int id)
{
XDocument xmlDocument = XDocument.Load ("Contacts.xml" )
return xmlDocument.Element ("Contacts" ).Elements ("Contact" )
.Select (x => int.Parse (x .Attribute ("Id" ).Value ))
.Where (y => y == id).Count () > 0
}
}
}
主窗体frmMain.cs
public partial class frmMain : Form
{
LinqXmlContactsBLManager blManager = new LinqXmlContactsBLManager()
public frmMain()
{
InitializeComponent()
//如果xml文件丢失,创建它
blManager.SetXmlFile ()
dataGridView1.DataSource = blManager.GetAllContacts ()
}
private void btnViewContact_Click(object sender, EventArgs e)
{
if (txtId.Text == "" )
{
MessageBox.Show ("请输入ID" , "消息" )
txtId.Focus ()
return
}
int x
if (!int.TryParse (txtId.Text , out x ))
{
MessageBox.Show ("请输入有效的ID" , "消息" )
txtId.Focus ()
txtId.SelectAll ()
return
}
if (!blManager.IdExists (int.Parse (txtId.Text )))
{
MessageBox.Show ("id " + txtId.Text + " 不存在" , "消息" )
txtId.Focus ()
txtId.SelectAll ()
return
}
//通过id获得一个联系人
Contact contact = blManager.GetContactById (int.Parse (txtId.Text ))
//show the contact properties on the controls
lblId.Text = contact.Id .ToString ()
txtFirstName.Text = contact.FirstName
txtLastName.Text = contact.LastName
txtCompany.Text = contact.Company
txtCity.Text = contact.City
txtTelephone.Text = contact.Telephone
txtFax.Text = contact.Fax
txtEmail.Text = contact.Email
txtId.Clear ()
}
private void btnNewContact_Click(object sender, EventArgs e)
{
frmAdd frmAdd1 = new frmAdd()
frmAdd1.ShowDialog ()
dataGridView1.DataSource = blManager.GetAllContacts ()
}
private void btnUpdateContact_Click(object sender, EventArgs e)
{
if (lblId.Text == "" )
{
MessageBox.Show ("未选择联系人" , "消息" )
txtId.Focus ()
txtId.SelectAll ()
return
}
if (txtFirstName.Text == "" )
{
MessageBox.Show ("请输入姓氏" , "消息" )
txtFirstName.Focus ()
return
}
if (txtLastName.Text == "" )
{
MessageBox.Show ("请输入名" , "消息" )
txtLastName.Focus ()
return
}
if (txtCompany.Text == "" )
{
MessageBox.Show ("请输入公司" , "消息" )
txtCompany.Focus ()
return
}
if (txtCity.Text == "" )
{
MessageBox.Show ("请输入城市" , "消息" )
txtCity.Focus ()
return
}
if (txtTelephone.Text == "" )
{
MessageBox.Show ("请输入电话" , "消息" )
txtTelephone.Focus ()
return
}
if (txtFax.Text == "" )
{
MessageBox.Show ("请输入传真" , "消息" )
txtFax.Focus ()
return
}
if (txtEmail.Text == "" )
{
MessageBox.Show ("请输入邮件" , "消息" )
txtEmail.Focus ()
return
}
//创建一个联系人,并从控件值设置他的属性
Contact contact = new Contact()
{
Id = int.Parse (lblId.Text ),
FirstName = txtFirstName.Text ,
LastName = txtLastName.Text ,
Company = txtCompany.Text ,
City = txtCity.Text ,
Telephone = txtTelephone.Text ,
Fax = txtFax.Text ,
Email = txtEmail.Text
}
//更新联系人
blManager.UpdateContact (contact)
dataGridView1.DataSource = blManager.GetAllContacts ()
}
private void btnDeleteContact_Click(object sender, EventArgs e)
{
if (lblId.Text == "" )
{
MessageBox.Show ("未选择联系人" , "消息" )
txtId.Focus ()
txtId.SelectAll ()
return
}
Contact contact = blManager.GetContactById (int.Parse (lblId.Text ))
string messageToPrint = "The following contact will get deleted:\n\n" +
"Id : " + contact.Id .ToString () + "\n" +
"First name : " + contact.FirstName + "\n" +
"Last name : " + contact.LastName + "\n" +
"Company : " + contact.Company + "\n" +
"City : " + contact.City + "\n" +
"Telephone : " + contact.Telephone + "\n" +
"Fax : " + contact.Fax + "\n" +
"Email : " + contact.Email + "\n\n" +
"Are you sure?"
DialogResult dialogResult = MessageBox.Show (messageToPrint, "Message" ,
MessageBoxButtons.YesNo )
//如果用户点击“否”,则取消删除操作
if (dialogResult == DialogResult.No )
{
MessageBox.Show ("操作被取消" , "消息" )
return
}
//如果用户点击“是”,删除联系人
blManager.DeleteContact (contact.Id )
dataGridView1.DataSource = blManager.GetAllContacts ()
//清除控件值
lblId.Text = ""
txtFirstName.Clear ()
txtLastName.Clear ()
txtCompany.Clear ()
txtCity.Clear ()
txtTelephone.Clear ()
txtFax.Clear ()
txtEmail.Clear ()
}
private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit ()
}
}
增加窗体frmAdd.cs
public partial class frmAdd : Form
{
LinqXmlContactsBLManager blManager = new LinqXmlContactsBLManager();
public frmAdd ()
{
InitializeComponent();
}
private void btnAdd_Click (object sender, EventArgs e)
{
if (txtFirstName.Text == "" )
{
MessageBox.Show("请输入姓氏" , "消息" );
txtFirstName.Focus();
return ;
}
if (txtLastName.Text == "" )
{
MessageBox.Show("请输入名字" , "消息" );
txtLastName.Focus();
return ;
}
if (txtCompany.Text == "" )
{
MessageBox.Show("请输入公司" , "消息" );
txtCompany.Focus();
return ;
}
if (txtCity.Text == "" )
{
MessageBox.Show("请输入城市" , "消息" );
txtCity.Focus();
return ;
}
if (txtTelephone.Text == "" )
{
MessageBox.Show("请输入电话" , "消息" );
txtTelephone.Focus();
return ;
}
if (txtFax.Text == "" )
{
MessageBox.Show("请输入传真" , "消息" );
txtFax.Focus();
return ;
}
if (txtEmail.Text == "" )
{
MessageBox.Show("请输入电子邮件" , "消息" );
txtEmail.Focus();
return ;
}
int id = blManager.GetNextId();
Contact contact = new Contact()
{
Id = id,
FirstName = txtFirstName.Text,
LastName = txtLastName.Text,
Company = txtCompany.Text,
City = txtCity.Text,
Telephone = txtTelephone.Text,
Fax = txtFax.Text,
Email = txtEmail.Text
};
blManager.AddContact(contact);
txtFirstName.Clear();
txtLastName.Clear();
txtCompany.Clear();
txtCity.Clear();
txtTelephone.Clear();
txtFax.Clear();
txtEmail.Clear();
txtFirstName.Focus();
}
private void btnClose_Click (object sender, EventArgs e)
{
Close();
}
}
运行结果如图: