• xml节点的添加和删除 Virus


    添加之前的结果

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

    【Blog】http://virusswb.cnblogs.com/

    【MSN】jorden008@hotmail.com

    【说明】转载请标明出处,谢谢

    反馈文章质量,你可以通过快速通道评论:

  • 相关阅读:
    特性标签的灵活使用
    算法实例题
    网络抓包工具
    vs2010
    .NET Remoting vs Web Service
    电子商务网站设计学习
    EXCEL导出
    C# 16进制与字符串、字节数组之间的转换
    DES加密
    DataGridView生成CSV,XML 和 EXCEL文件
  • 原文地址:https://www.cnblogs.com/virusswb/p/1283995.html
Copyright © 2020-2023  润新知