/// <summary>
/// 插入xml文档
/// </summary>
public static void InsertXml(string PageHeads)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("PageList.xml");//加载文档
XmlNode root = xmlDoc.SelectSingleNode("fatherPage");//查找<fatherPage></fatherPage>
XmlElement xe1 = null;
XmlNodeList xnl = root.ChildNodes;//获取所有子节点
//声明一个节点存储根节点
XmlNode movie = xmlDoc.DocumentElement;
string name = PageHeads;//页面头部文字
//System.Windows.Forms.MessageBox.Show("cbxmlclass:"+name);
int count = 0;
foreach (XmlNode xn in xnl)
{//遍历根节点下的子节点
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("cbName") == name)
{//显示属性值
//MessageBox.Show(xe.GetAttribute("cbName"));
count += 1;//记录是否有重复值
}
}
if (count > 0)//有两条相同的记录返回
return;
else
{
xe1 = xmlDoc.CreateElement("sonPage");//创建一个<sonPage></sonPage>节点
//xnl.Count+1节点的最大值加1
xe1.SetAttribute("cbId", xnl.Count + 1 + "");//设置该节点cbId属性
xe1.SetAttribute("cbName", name);//设置该节点cbName属性
root.AppendChild(xe1);//添加到<fatherPage></fatherPage>节点中
xmlDoc.Save("PageList.xml");
}
}
输出结果:
<?xml version="1.0" encoding="gb2312"?>
<fatherPage>
<sonPage cbId="1" cbName="线路容量历史数据华南甲线" />
<sonPage cbId="2" cbName="线路容量历史数据北南甲线" />
</fatherPage>
/// <summary>
/// 删除xml属性
/// </summary>
private void DelXml()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("PageList.xml");
XmlNode xn = xmlDoc.SelectSingleNode("fatherPage");
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
xe.GetAttribute("cbId");//显示属性值
if (xe.GetAttribute("cbId") == id.ToString())
{
xe.RemoveAttribute("cbId");//删除genre属性
}
else if (xe.GetAttribute("cbName") == pageHeadtext)
{
xe.RemoveAll();//删除该节点的全部内容
}
}
}
//这个类是查询线路名,如果有相同的线路名,在Dockpanel 返回不处理,还要查询是否打开哪个线路名对面的页面,否则打开新页面
public void isSTRAIN_HISTORY()
{//耐张段历史数据表
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("PageList.xml");//加载文档
XmlNode root = xmlDoc.SelectSingleNode("fatherPage");//查找<fatherPage></fatherPage>
XmlNodeList xnl = root.ChildNodes;//获取所有子节点
//声明一个节点存储根节点
XmlNode movie = xmlDoc.DocumentElement;
string name = SelObjChoice.pageHeadtext;
//MessageBox.Show(name);
int count = 0;
foreach (XmlNode xn in xnl)
{//遍历根节点下的子节点
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("cbName") == name)
{//显示属性值
count += 1;//记录是否有重复值
}
}
//MessageBox.Show(count.ToString());
if (count > 0 && SMidst != null)//有两条相同的记录返回
{
if (SMidst.IsDisposed)
SMidst = new SMidSegmenTvolume();//如果已经摧毁,则重新创建子窗体
SMidst.Show(dockPanel1);
}
else
{
SMidst = new SMidSegmenTvolume();
SMidst.Show(dockPanel1);
}
}