代码改变世界

xml节点的添加和删除

  Virus-BeautyCode  阅读(747)  评论(0编辑  收藏  举报
添加之前的结果

xmlNodeDelete.JPG

添加之后的结果
xmlNodeInsert.JPG
删除之后就和添加之前是一样的了,呵呵

代码如下:

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace WindowsApp
{
    
public partial class XMLForm2 : Form
    {
        
private string _xmlstring=@"<blockList version='1'>
  <disk id='disk001'/>
  <disk id='disk002'/>
 </blockList>
";

        
public XMLForm2()
        {
            InitializeComponent();
        }
        
private void display(string xml)
        {
            richTextBox1.Clear();
            XmlDocument xmlDoc 
= new XmlDocument();
            xmlDoc.LoadXml(xml);
            _xmlstring 
= xmlDoc.OuterXml;
            XmlNodeList nodes 
= xmlDoc.SelectNodes("blockList/disk");
            
foreach (XmlNode node in nodes)
            {
                richTextBox1.AppendText(node.Attributes[
"id"].Value);
                richTextBox1.AppendText(Environment.NewLine);
            }
        }
        
/// <summary>
        
/// 更新安全策略,产生新的阻断列表
        
/// </summary>
        
/// <param name="kbid">U盘金邦ID</param>
        
/// <param name="flag">产生标志:1添加一个U盘,2删除一个U盘</param>
        public void Update(string kbid, int flag,ref string blockList)
        {
            
            
switch (flag)
            {
                
case 1:

                    insertDisk(kbid,
ref blockList);
                    
break;
                
case 2:

                    deleteDisk(kbid,
ref blockList);
                    
break;
            }
            
        }
        
/// <summary>
        
/// 添加一个U盘到阻断列表
        
/// </summary>
        
/// <param name="kbid">U盘金邦ID</param>
        
/// <param name="blockList">阻断列表</param>
        private void insertDisk(string kbid,ref string blockList)
        {
            XmlDocument xmlDoc 
= new XmlDocument();
            xmlDoc.LoadXml(blockList);
            
if (!contains(kbid, xmlDoc))
            {
                XmlNode root 
= xmlDoc.SelectSingleNode("/blockList");
                XmlElement node 
= xmlDoc.CreateElement("disk");
                node.SetAttribute(
"id", kbid);
                root.AppendChild(node);
                blockList 
= xmlDoc.OuterXml;
            }
        }
        
/// <summary>
        
/// 从阻断列表中删除一个U盘
        
/// </summary>
        
/// <param name="kbid">U盘金邦ID</param>
        
/// <param name="blockList">阻断列表</param>
        private void deleteDisk(string kbid,ref string blockList)
        {
            XmlDocument xmlDoc 
= new XmlDocument();
            System.IO.StringWriter sw 
= new System.IO.StringWriter();
            xmlDoc.LoadXml(blockList);
            
if (contains(kbid, xmlDoc))
            {
                XmlNode disk 
= xmlDoc.SelectSingleNode("/blockList/disk[@id='" + kbid + "']");
                XmlNode root 
= xmlDoc.SelectSingleNode("/blockList");
                root.RemoveChild(disk);
                xmlDoc.Save(sw);
                blockList 
= sw.ToString();
            }
        }
        
/// <summary>
        
/// 判断阻断列表中是否存在指定U盘
        
/// </summary>
        
/// <param name="kbid">U盘金邦ID</param>
        
/// <param name="blockList">阻断列表</param>
        
/// <returns></returns>
        private bool contains(string kbid, XmlDocument blockList)
        {
            XmlNode disk 
= blockList.SelectSingleNode("/blockList/disk[@id='"+kbid+"']");
            
if (disk == null)
                
return false;
            
else
                
return true;
        }

        
private void XMLForm2_Load(object sender, EventArgs e)
        {
            display(_xmlstring);
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            
this.Update("kb999"1ref _xmlstring);
            display(_xmlstring);
        }

        
private void button2_Click(object sender, EventArgs e)
        {
            
this.Update("kb999"2ref _xmlstring);
            display(_xmlstring);
        }
    }
}
复制代码
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2007-09-04 [原创]包头人在北京<一>
点击右上角即可分享
微信分享提示