.Net高级技术 第五章:xml

XML 教程

http://www.w3school.com.cn/xml/index.asp

1.读取XML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace _01_XML
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载
            XDocument xdoc = XDocument.Load("first.xml");
            XElement xel= xdoc.Root; //根元素
            //Console.WriteLine(xele);
            IEnumerable<XElement> ies= xel.Elements();
            foreach (XElement item in ies)
            {
                //Console.WriteLine(item.Name);
                //获取属性的值
                Console.WriteLine("======" + item.Name + ":" + item.Attribute("id").Value + "======");
                //获取student元素下的所有的元素的值
                Console.WriteLine(item.Element("name").Value);
                Console.WriteLine(item.Element("gender").Value);
                Console.WriteLine(item.Element("age").Value);
            }
            Console.ReadKey();
        }
    }
}
first.xml
<?xml version="1.0" encoding="utf-8" ?>
<class>
    <student id="0001">
        <name>yokin</name>
        <gender></gender>
        <age>25</age>
    </student>
    <student id="0002">
        <name>linyq</name>
        <gender></gender>
        <age>29</age>
    </student>
    <student id="0003">
        <name>linyongqin</name>
        <gender></gender>
        <age>20</age>
    </student>
</class>

2.写入XML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace _02_XML写入
{
    class Program
    {
        static void Main(string[] args)
        {
            XDocument xdoc = new XDocument();
            //根元素
            XElement root = new XElement("school");
            //子元素
            XElement cls = new XElement("class");
            //设置class中的属性
            cls.SetAttributeValue("id", "001");
            //创建一个student标签
            XElement stu = new XElement("student");
            //设置student标签中的一些元素和值
            stu.SetElementValue("name", "yokin");
            stu.SetElementValue("gender", "男");
            stu.SetElementValue("age", 29);
            //标签添加到子元素中
            cls.Add(stu);
            //子元素添加到根元素中
            root.Add(cls);
            //根元素添加到xdoc
            xdoc.Add(root);
            //保存
            xdoc.Save("");
            Console.WriteLine("写入成功");
            Console.ReadKey();
        }
    }
}

  MySchool.xml

<?xml version="1.0" encoding="utf-8"?>
<school>
  <class id="001">
    <student>
      <name>yokin</name>
      <gender></gender>
      <age>29</age>
    </student>
  </class>
</school>

3.XML 增删改查CRUD

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadFiles();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadFiles()
        {
            List<StudentModel> list = new List<StudentModel>();
            XDocument xDoc = XDocument.Load("student.xml");
            XElement root = xDoc.Root;
            foreach (XElement item in root.Elements())
            {
                StudentModel model = new StudentModel();
                model.stuID = item.Attribute("id").Value;
                model.stuName = item.Element("name").Value;
                model.stuGender = item.Element("gender").Value;
                model.stuAge = item.Element("age").Value;
                list.Add(model);
            }
            gvwList.DataSource = list;
            if (list.Count > 0)
            {
                gvwList.SelectedRows[0].Selected = false;
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            XDocument xDoc = XDocument.Load("student.xml");
            XElement root = xDoc.Root;
            XElement student = new XElement("student");
            student.SetAttributeValue("id", txtID.Text);
            student.SetElementValue("name", txtName.Text);
            string gender = rbtnMan.Checked ? "男" : "女";
            student.SetElementValue("gender", gender);
            student.SetElementValue("age", txtAge.Text);
            root.Add(student);
            xDoc.Save("student.xml");
            LoadFiles();
            txtID.Text = "";
            txtName.Text = "";
            rbtnMan.Checked = true;
            txtAge.Text = "";
            MessageBox.Show("新增成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void delete_Click(object sender, EventArgs e)
        {
            if (gvwList.SelectedRows.Count > 0)
            {
                string id = gvwList.SelectedRows[0].Cells[0].Value.ToString();
                XDocument xDoc = XDocument.Load("student.xml");
                XElement root = xDoc.Root;
                XElement student = root.Elements("student").Where(x => x.Attribute("id").Value == id).Single();
                student.Remove();
                xDoc.Save("student.xml");
                LoadFiles();
                MessageBox.Show("删除成功");
            }
            else
            {
                MessageBox.Show("请选择要删除的行");
            }
        }

        /// <summary>
        /// 获取选中对象的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void update_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = gvwList.SelectedRows[0];
            txtID.Text = row.Cells[0].Value.ToString();
            txtID.ReadOnly = true;
            txtName.Text = row.Cells[1].Value.ToString();
            if (row.Cells[2].Value.ToString()=="男")
            {
                rbtnMan.Checked = true;
            }
            else
            {
                rbtnWoman.Checked = true;
            }
            txtAge.Text = row.Cells[3].Value.ToString();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            XDocument xDoc = XDocument.Load("student.xml");
            XElement root = xDoc.Root;
            XElement student = root.Elements("student").Where(x => x.Attribute("id").Value.ToString() == txtID.Text).Single();
            student.SetElementValue("name", txtName.Text);
            string gender = rbtnMan.Checked ? "男" : "女";
            student.SetElementValue("gender", gender);
            student.SetElementValue("age", txtAge.Text);
            xDoc.Save("student.xml");
            LoadFiles();
            txtID.Text = "";
            txtID.ReadOnly = false;
            txtName.Text = "";
            rbtnMan.Checked = true;
            txtAge.Text = "";
            MessageBox.Show("修改成功");
        }
    }
}

  student.xml

<?xml version="1.0" encoding="gb2312"?>
<class>
  <student id="001">
    <name>卡卡西</name>
    <gender></gender>
    <age>23</age>
  </student>
  <student id="002">
    <name>大蛇丸</name>
    <gender></gender>
    <age>42</age>
  </student>
  <student id="003">
    <name>鸣人</name>
    <gender></gender>
    <age>16</age>
  </student>
  <student id="004">
    <name>小花</name>
    <gender></gender>
    <age>15</age>
  </student>
</class>

 

 

  

posted @ 2015-05-14 21:55  linyongqin  阅读(131)  评论(0编辑  收藏  举报