C# Winform XML

本文介绍如何使用C#进行XML文档的基本操作,包括插入新的XML元素、删除特定属性及查询节点等。通过具体代码实例,展示了如何确保数据的一致性和正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        /// <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);
            }

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值