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.Xml.Linq;
using System.Xml;
using lib_Project;
namespace CinemaSystem
{
public partial class
FrmNewAddMovie : Form
{
public FrmNewAddMovie()
{
InitializeComponent();
}
private void btnOpenImage_Click(object sender,
EventArgs e)
{//打开图片
if
(openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.ptbMovie.ImageLocation =
openFileDialog1.FileName;
}
}
private void btnSave_Click(object sender,
EventArgs e)
{
//保存图片,并且获取路径
if
(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string path =
Application.ExecutablePath.Substring(0,Application.ExecutablePath.LastIndexOf('\\')+1);
string imagePath = path +
"Image\\";
//MessageBox.Show(imagePath);
this.ptbMovie.Image.Save(imagePath+saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.LastIndexOf('\\')+1));
}
}
private void btnAdd_Click(object sender,
EventArgs e)
{
//将信息添加到XML文件中
var xml =
XElement.Load("xmlShowList.xml");
XElement
NewInfo = new XElement("Movie",
new XAttribute("name",
txtMovieName.Text),
new XAttribute("director",
txtDirector.Text),
new XAttribute("actor",
txtImportant.Text),
new XAttribute("movietype",
cmbType.Text),
new XAttribute("price",
txtOldPrice.Text),
new XAttribute("poster",
saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.LastIndexOf('\\')
+ 1))
);
List<string> name =
xml.Elements("Movie").Select(n =>
n.Attribute("name").Value).Distinct().ToList();
foreach
(var item in name)
{
if
(item.Equals(NewInfo.Attribute("name").Value))
{
MessageBox.Show("您添加的电影已存在!");
return;
}
}
xml.Add(NewInfo);
MessageBox.Show("添加成功!");
xml.Save("xmlShowList.xml");
}