Example #1
0
        /// <summary>
        /// 修改一条信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Modify_Click(object sender, EventArgs e)
        {
            XmlDocument document = new XmlDocument();

            document.Load("BookXML.xml");

            XmlElement xe       = document.DocumentElement;                                    // DocumentElement 获取xml文档对象的根XmlElement.
            string     strPath  = string.Format("/bookstore/book[@ISBN=\"{0}\"]", dataGridView1.CurrentRow.Cells[1].Value.ToString());
            XmlElement selectXe = (XmlElement)xe.SelectSingleNode(strPath);                    //selectSingleNode 根据XPath表达式,获得符合条件的第一个节点.

            selectXe.SetAttribute("Type", dataGridView1.CurrentRow.Cells[0].Value.ToString()); //也可以通过SetAttribute来增加一个属性
            selectXe.GetElementsByTagName("title").Item(0).InnerText  = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            selectXe.GetElementsByTagName("author").Item(0).InnerText = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            selectXe.GetElementsByTagName("price").Item(0).InnerText  = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            document.Save("Book.xml");

            //Linq 修改数据
            LinqXML.LinqXMLModifyFunc(dataGridView1);
            MessageBox.Show("修改成功!");
        }