xml文件:
<?xml version="1.0" encoding="utf-8"?>
<AutoPrintProject>
<Project>
<ProjectName>SPA Reminder</ProjectName>
<Status>Disable</Status>
<LastPrintTime>2011-4-3 11:00:00</LastPrintTime>
<Daily>1</Daily>
<StartTime>2011/4/10 14:04:49</StartTime>
<Printer>Microsoft XPS Document Writer</Printer>
<SavePath>E:\IFCA\R2.8\PropertyNet\Bin</SavePath>
<AllField>CompanyNo,ProjectName,Projectno,PhaseName,BlockName ,unitno
</AllField>
<letterid>51</letterid>
<Operation>Equal,Greater,Not Equal,Like,Less</Operation>
<WhereField>
<Where>
<WhereField>BlockName</WhereField>
<WhereOperator>like</WhereOperator>
<WhereValue>%陶%</WhereValue>
</Where>
<Where>
<WhereField>unitno</WhereField>
<WhereOperator>Equal</WhereOperator>
<WhereValue>0010</WhereValue>
</Where>
<Where>
<WhereField>CompanyNo</WhereField>
<WhereOperator>Equal</WhereOperator>
<WhereValue>10141</WhereValue>
</Where>
<Where>
<WhereField>PhaseName</WhereField>
<WhereOperator>Equal</WhereOperator>
<WhereValue>422</WhereValue>
</Where>
</WhereField>
</Project>
</AutoPrintProject>
//读xml到table中
public DataTable ReadXlm()
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("AutoPrintProject.xml");
XmlNode xn = xmldoc.SelectSingleNode("AutoPrintProject");
XmlNodeList list = xn.ChildNodes;
DataTable ta = new DataTable();
ta.Columns.Add("JobName", typeof(string));
ta.Columns.Add("Status", typeof(string));
ta.Columns.Add("LastExecuteTime", typeof(string));
foreach (XmlNode node in list)
{
XmlElement element = (XmlElement)node;
DataRow row = ta.NewRow();
row["JobName"] = element["ProjectName"].InnerText.ToString().Trim();
row["Status"] = element["Status"].InnerText.ToString().Trim();
row["LastExecuteTime"] = element["LastPrintTime"].InnerText.ToString().Trim();
ta.Rows.Add(row);
}
return ta;
}
//修改xml某个节点的值
private void Start_Click(object sender, EventArgs e)
{
try
{
string modifyvalue = dataGridView1.CurrentRow.Cells["JobName"].Value.ToString().Trim();
if (modifyvalue == "")
{
MessageBox.Show("请选择项目!");
return;
}
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("AutoPrintProject.xml");
XmlElement xn = (XmlElement)xmldoc.SelectSingleNode("AutoPrintProject/Project[ProjectName='" + modifyvalue + "']");
if (xn.GetElementsByTagName("Status").Item(0).InnerText != "Start")
{
xn.GetElementsByTagName("Status").Item(0).InnerText = "Start";
xmldoc.Save("AutoPrintProject.xml");
MessageBox.Show("工作启动成功!");
}
else
{
MessageBox.Show("该工作已经启动!");
}
refresh();
}
catch (Exception ex)
{
MessageBox.Show("启动失败,原因:" + ex.Message.Trim());
}
}
//添加一个节点
private void save_Click(object sender, EventArgs e)
{
try
{
if (ProjectName == "")
{
MessageBox.Show("请选择项目!");
return;
}
string selectedfield=drop_field.SelectedValue.ToString().Trim();
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("AutoPrintProject.xml");
XmlNode xn = xmldoc.SelectSingleNode("AutoPrintProject/Project[ProjectName='" + ProjectName + "']");
if ((xn!=null)&&(!CheckField(selectedfield)))
{
XmlNode xmlnode=(XmlNode)xn.SelectSingleNode("WhereField");
XmlElement xe1=xmldoc.CreateElement("Where");//创建一个节点
XmlNode subfieldwhere = xmldoc.CreateElement("WhereField");
subfieldwhere.InnerText = selectedfield;
xe1.AppendChild(subfieldwhere);
string selectedoperator = drop_operator.SelectedValue.ToString().Trim();
XmlNode suboperatorwhere = xmldoc.CreateElement("WhereOperator");
suboperatorwhere.InnerText = selectedoperator;
xe1.AppendChild(suboperatorwhere);
string selectedvalue = tb_value.Text.Trim();
XmlNode subvaluewhere = xmldoc.CreateElement("WhereValue");
subvaluewhere.InnerText = selectedvalue;
xe1.AppendChild(subvaluewhere);
xmlnode.AppendChild(xe1);
xmldoc.Save("AutoPrintProject.xml");
MessageBox.Show("增加字段成功!");
this.Close();
parent.Refresh();
//Criteria criteria = new Criteria();
//criteria.ProjectName = ProjectName;
//criteria.refresh();
}
else
{
MessageBox.Show("该字段已经存在!");
}
}
catch (Exception ex)
{
MessageBox.Show("增加字段失败,原因:" + ex.Message.Trim());
}
}
//删除一个节点
private void DeleteField_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
DialogResult dr = MessageBox.Show("确定删除吗?", "删除", messButton);
try
{
if (dr == DialogResult.OK)//如果点击
{
if (ProjectName == "")
{
MessageBox.Show("请选择项目!");
return;
}
string selectedfield = dataGridView1.CurrentRow.Cells["Field"].Value.ToString().Trim();
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("AutoPrintProject.xml");
XmlNode xn = xmldoc.SelectSingleNode("AutoPrintProject/Project[ProjectName='" + ProjectName + "']");
if (xn != null)
{
XmlNode xmlnode = (XmlNode)xn.SelectSingleNode("WhereField");
XmlNodeList nodelist = xmlnode.ChildNodes;
foreach (XmlNode one in nodelist)
{
XmlElement element = (XmlElement)one;
if (element.GetElementsByTagName("WhereField").Item(0).InnerText.Trim() == selectedfield)
{
xmlnode.RemoveChild(one);
}
}
xmldoc.Save("AutoPrintProject.xml");
MessageBox.Show("删除字段成功!");
refresh();
}
else
{
MessageBox.Show("删除字段失败!");
}
}
}
catch (Exception ex)
{
MessageBox.Show("删除字段失败,原因:" + ex.Message.Trim());
}
}