zoukankan      html  css  js  c++  java
  • 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

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

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

  • 相关阅读:
    [daily][netcat] 在UNIX socket上使用netcat
    [emacs] emacs设置python code的indent
    [dev][python] 从python2进阶到python3你都需要了解什么
    [strongswan][autoconf][automake][cento] 在CentOS上编译strongswan git源码时遇到的autoconf问题
    [strongswan] strongswan是如何实现与xfrm之间的trap机制的
    对不可描述的软件安装sfbo插件
    [daily] 如何用emacs+xcscope阅读内核源码
    [daily] cscope
    [dev][ipsec] 什么是xfrm
    [dev][ipsec] netlink是什么
  • 原文地址:https://www.cnblogs.com/virusswb/p/1283995.html
Copyright © 2011-2022 走看看