Winform中的linq to XML和listview

来源:互联网 发布:ipad淘宝怎么开店 编辑:程序博客网 时间:2024/04/30 13:58
 
           //更改属性
            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("", 0);
            this.listView1.Columns.Add("列1",80);
            this.listView1.Columns.Add("列2", 160);
            //添加各项
            ListViewItem[] p = new ListViewItem[2];
            p[0] = new ListViewItem(new string[] { "","aaaa","bbbb"});
            p[1] = new ListViewItem(new string[] { "","cccc", "ggggg" });
            //p[0].SubItems[0].BackColor = Color.Red; //用于设置某行的背景颜色

            this.listView1.Items.AddRange(p);
            //也可以用this.listView1.Items.Add();不过需要在使用的前后添加Begin... 和End...防止界面自动刷新
            // 添加分组
            this.listView1.Groups.Add(new ListViewGroup("tou"));
            this.listView1.Groups.Add(new ListViewGroup("wei"));

            this.listView1.Items[0].Group = this.listView1.Groups[0];
            this.listView1.Items[1].Group = this.listView1.Groups[1];

 

此例子实现了linq to XML增删改查的基本功能,而初始化的Form1_Load中应用了ListView,用了两种方法绑定。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Collections.Specialized;


namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strNow = DateTime.Now.ToString("yyyyMMddhhmmss");

            FileInfo fiXML = new FileInfo(@"XML\xmlLog.xml");//在这个project的bin/debug/XML/xmllog.xml中。
            if (!(fiXML.Exists))
            {//创建文档
                XDocument xelLog = new XDocument(
                    new XDeclaration("1.0", "utf-8", "no"),
                    new XElement("ipmsg",
                        new XElement("msg_log",
                            new XElement("user", "Bin"),
                            new XElement("logdate", strNow),
                            new XElement("message", "一条xml to linq测试")
                        )
                     )
                 );
                xelLog.Save(@"XML\xmlLog.xml");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strNow = DateTime.Now.ToString("yyyyMMddhhmmss");
            Dictionary<string, string> dicLog = new Dictionary<string, string>();
            dicLog.Add("user", "Bin");
            dicLog.Add("logdate", strNow);
            dicLog.Add("message", "这是一条添加记录测试");


            XElement xelem = XElement.Load(@"XML\xmlLog.xml");
            XElement newLog = new XElement("msg_log",
                                  new XElement("user", (string)dicLog["user"]),
                                  new XElement("logdate", (string)dicLog["logdate"]),
                                  new XElement("message", (string)dicLog["message"])
                                  );
            xelem.Add(newLog);
            xelem.Save(@"XML\xmlLog.xml");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            XElement xelem = XElement.Load(@"XML\xmlLog.xml");

            var queryXML = from xmlLog in xelem.Descendants("msg_log")
                           where Convert.ToInt64(xmlLog.Element("logdate").Value) < 20110829041321
                           select xmlLog;

            queryXML.Remove();
            xelem.Save(@"XML\xmlLog.xml");
        }

        private void button1_Click_1(object sender, EventArgs e)
        {  XElement xelem = XElement.Load(@"XML\xmlLog.xml");
            var queryXML = from xmlLog in xelem.Descendants("msg_log")
                           where xmlLog.Element("user").Value == "Bin"
                           select xmlLog;

            foreach (XElement el in queryXML)
            {
                el.Element("user").Value = "LiuBin";//开始修改
            }
            xelem.Save(@"XML\xmlLog.xml");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            XElement xelem = XElement.Load(@"XML\xmlLog.xml");
            var queryXML = from xmlLog in xelem.Descendants("msg_log")
                           where xmlLog.Element("user").Value == "LiuBin"
                           select xmlLog;

            this.listView1.View = View.Details;
            this.listView1.GridLines = true;
            this.listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
            listView1.Columns.Add("用户名", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("日期",100,HorizontalAlignment.Center);
            listView1.Columns.Add("消息",200, HorizontalAlignment.Center);
            this.listView1.Visible = true;

            //MessageBox.Show("zhi:" + queryXML.ElementAt(0).Element("user").Value);

            /*for (int i = 0; i < queryXML.Elements("user").Count(); i++)
            {
                ListViewItem item = new ListViewItem();
                item.Text = queryXML.ElementAt(i).Element("user").Value;
                item.SubItems.Add(queryXML.ElementAt(i).Element("logdate").Value);
                item.SubItems.Add(queryXML.ElementAt(i).Element("message").Value);
                listView1.Items.Add(item);
            }*/
            foreach (XElement el in queryXML)
            {
                ListViewItem item = new ListViewItem();
                item.Text = el.Element("user").Value;
                item.SubItems.Add(el.Element("logdate").Value);
                item.SubItems.Add(el.Element("message").Value);
                listView1.Items.Add(item);
            }
        }
    }
}

原创粉丝点击