zoukankan      html  css  js  c++  java
  • 【源码】操作xml Form1

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using Zone;
    using System.Runtime.InteropServices;


    namespace 操作xml
    {
    public partial class Form1 : Form
    {
    private int ListItemIndex = -1;
    [DllImport("user32")]
    private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

    public Form1()
    {
    InitializeComponent();
    this.listView1.GridLines = true; //显示表格线
    this.listView1.View = View.Details;//显示表格细节
    this.listView1.LabelEdit = true; //是否可编辑,ListView只可编辑第一列。
    this.listView1.Scrollable = true;//有滚动条
    this.listView1.HeaderStyle = ColumnHeaderStyle.Clickable;//对表头进行设置
    this.listView1.FullRowSelect = true;//是否可以选择行


    //this.listView1.HotTracking = true;// 当选择此属性时则HoverSelection自动为true和Activation属性为oneClick
    //this.listView1.HoverSelection = true;
    //this.listView1.Activation = ItemActivation.Standard; //
    //添加表头
    this.listView1.Columns.Add("别名",120);
    this.listView1.Columns.Add("路径",450);

    XmlFasta.CreateXml();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    string s1 = textBox1.Text;
    string s2 = textBox2.Text;

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("data.xml");

    XmlNode root = xmlDoc.SelectSingleNode("Element");

    XmlElement xe1 = xmlDoc.CreateElement("alias");
    xe1.SetAttribute("alias", s1);
    xe1.SetAttribute("cmd", s2);

    root.AppendChild(xe1);
    xmlDoc.Save("data.xml");

    }

    private void Form1_Load(object sender, EventArgs e)
    {
    Loadxml();
    //GetXml();
    LoadListView();
    }


    //**************************修改*****************************
    private void button2_Click(object sender, EventArgs e)
    {
    ListViewItem p = new ListViewItem();
    p = listView1.Items[ListItemIndex];

    string ss1 = p.SubItems[0].Text;
    string ss2 = p.SubItems[1].Text;



    string s1 = textBox1.Text.Trim();
    string s2 = textBox2.Text.Trim();


    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("data.xml");

    XmlNodeList nodeList = xmlDoc.SelectSingleNode("Element").ChildNodes;

    foreach (XmlNode xn in nodeList)
    {
    XmlElement xe = (XmlElement)xn;
    if (xe.GetAttribute("alias") == ss1 && xe.GetAttribute("cmd") == ss2)
    {
    xe.SetAttribute("alias", s1);
    xe.SetAttribute("cmd", s2);
    break;
    }
    }
    textBox1.Text = "";
    textBox2.Text = "";

    xmlDoc.Save("data.xml");
    LoadListView(ListItemIndex);
    //SendMessage(listView1.Handle, 500, 200, 200);
    }

    void Loadxml()
    {
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("data.xml");


    XmlNode xn = xmlDoc.SelectSingleNode("Element");

    XmlNodeList xnl = xn.ChildNodes;

    foreach (XmlNode xnf in xnl)
    {
    XmlElement xe = (XmlElement)xnf;
    //Console.WriteLine(xe.GetAttribute("alias"));//显示属性值
    //Console.WriteLine(xe.GetAttribute("cmd"));

    XmlNodeList xnf1 = xe.ChildNodes;
    foreach (XmlNode xn2 in xnf1)
    {
    //Console.WriteLine(xn2.InnerText);//显示子节点点文本
    }
    }
    }
    void LoadListView()
    {
    LoadListView(0);
    }
    void LoadListView(int i)
    {
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("data.xml");


    XmlNode xn = xmlDoc.SelectSingleNode("Element");

    XmlNodeList xnl = xn.ChildNodes;

    ListViewItem p = new ListViewItem();
    listView1.Items.Clear();
    foreach (XmlNode xnf in xnl)
    {
    XmlElement xe = (XmlElement)xnf;
    p = new ListViewItem(new string[] { xe.GetAttribute("alias"), xe.GetAttribute("cmd") });
    this.listView1.Items.Add(p);
    }
    try
    {
    listView1.Items[i].Selected = true;
    listView1.Items[i].EnsureVisible();
    }
    catch
    {
    i = i - 1;
    listView1.Items[i].Selected = true;
    listView1.Items[i].EnsureVisible();
    }
    }


    private void toolStripMenuItem1_Click(object sender, EventArgs e)
    {
    ListViewItem p = new ListViewItem();
    p = listView1.Items[ListItemIndex];
    textBox1.Text = p.SubItems[0].Text;
    textBox2.Text = p.SubItems[1].Text;

    }






    //***************删除********************************************
    private void toolStripMenuItem2_Click(object sender, EventArgs e)
    {
    ListViewItem p = new ListViewItem();
    p = listView1.Items[ListItemIndex];

    string s1 = p.SubItems[0].Text;
    string s2 = p.SubItems[1].Text;


    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("data.xml");

    XmlNodeList xnl = xmlDoc.SelectSingleNode("Element").ChildNodes;

    foreach (XmlNode xn in xnl)
    {
    XmlElement xe = (XmlElement)xn;
    if (xe.GetAttribute("alias") == s1 && xe.GetAttribute("cmd") ==s2)
    {
    xe.ParentNode.RemoveChild(xe);
    break;
    }
    }
    xmlDoc.Save("data.xml");
    LoadListView(ListItemIndex);
    //SendMessage(listView1.Handle, 500, 200, 200);
    }

    private void listView1_Click(object sender, EventArgs e)
    {
    for (int i = 0; i < listView1.Items.Count; i++)
    {
    if (listView1.Items[i].Selected)
    {
    ListItemIndex = i;
    }
    }
    }
    }
    }
  • 相关阅读:
    xp sp3下 IIS5.1 配置python 的正确方法
    asp 采集不到数据,采集不成功 拒绝访问 msxml3.dll 错误 '80070005' 的解决方法
    列表样式
    css文本属性
    css背景属性
    常规流练习
    盒模型练习
    定位体系相关练习
    层叠机制的步骤
    简单网页的制作——html
  • 原文地址:https://www.cnblogs.com/chusiping/p/2251154.html
Copyright © 2011-2022 走看看