码迷,mamicode.com
首页 > 其他好文 > 详细

linq to xml

时间:2018-01-21 00:11:38      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:name   load   select   setattr   rem   for   attribute   ble   val   

static string strPath = "Employee.xml";

添加元素

        //添加XML元素
        private void button2_Click(object sender, EventArgs e)
        {
            XElement xe = XElement.Load(strPath);
            IEnumerable<XElement> elements1 = from element in xe.Elements("People")
                                              select element;
            //生成新的编号
            string str = (Convert.ToInt32(elements1.Max(element => element.Attribute("ID").Value)) + 1).ToString("000");
            XElement people = new XElement(
                "People", new XAttribute("ID", str),
                new XElement("Name", textBox11.Text),
                new XElement("Sex", comboBox1.Text),
                new XElement("Salary", textBox12.Text)
                );
            xe.Add(people);
            xe.Save(strPath);
            getXmlInfo();
        }

//修改XML元素

 //修改XML元素
        private void button3_Click(object sender, EventArgs e)
        {
            if (strID != "")
            {
                XElement xe = XElement.Load(strPath);
                IEnumerable<XElement> elements = from element in xe.Elements("People")
                                                 where element.Attribute("ID").Value == strID
                                                 select element;
                if (elements.Count() > 0)
                {
                    XElement newXE = elements.First();
                    newXE.SetAttributeValue("ID", strID);
                    newXE.ReplaceNodes(
                        new XElement("Name", textBox11.Text),
                        new XElement("Sex", comboBox1.Text),
                        new XElement("Salary", textBox12.Text)
                        );
                }
                xe.Save(strPath);
            } 
            getXmlInfo();
        }

删除元素

 //删除XML元素
        private void button4_Click(object sender, EventArgs e)
        {
            if (strID != "")
            {
                XElement xe = XElement.Load(strPath);
                IEnumerable<XElement> elements = from element in xe.Elements("People")
                                                 where element.Attribute("ID").Value == strID
                                                 select element;
                if (elements.Count() > 0)
                    elements.First().Remove();
                xe.Save(strPath);
            }
            getXmlInfo();
        }

显示详细信息

 //显示选中XML节点的详细信息
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            strID = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
            XElement xe = XElement.Load(strPath);
            IEnumerable<XElement> elements = from PInfo in xe.Elements("People")
                                             where PInfo.Attribute("ID").Value == strID
                                             select PInfo;
            foreach (XElement element in elements)
            {
                textBox11.Text = element.Element("Name").Value;
                comboBox1.SelectedItem = element.Element("Sex").Value;
                textBox12.Text = element.Element("Salary").Value;
            }
        }

 

linq to xml

标签:name   load   select   setattr   rem   for   attribute   ble   val   

原文地址:https://www.cnblogs.com/siyunianhua/p/8322255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!