dataGridView读取xml文件

有两种方法,推荐第二种,不同的地方已经加粗加大了。
第一种方法:
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; namespace WindowsFormsApp13 { public partial class Form1 : Form { public Form1() { InitializeComponent(); CreateXml(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { //没有列名的方法 //DataSet ds = new DataSet(); //ds.ReadXml("try.xml"); //this.dataGridView1.DataSource = ds.Tables[0]; //含有列名的方法 //中心思想就是创建DataSet对象,给DataSet对象的table添加DataTable对象,给DataTable对象添加列名,在添加DataRow对象,查找获得xml文件的节点值,给DataRow循环赋值 DataTable dt = new DataTable(); //添加列名 dt.Columns.Add("书名", typeof(string)); dt.Columns.Add("作者", typeof(string)); dt.Columns.Add("等级", typeof(string)); XmlDocument doc= new XmlDocument(); doc.Load("try.xml"); //先获得根节点 XmlElement root= doc.DocumentElement; //根据根节点,获得所有的子节点 XmlNodeList xmns=root.ChildNodes; //获得子节点的各种值,给每一行赋值。 foreach (XmlNode x in xmns) { //给dt表创建行,name为节点的第一个,auther为第二个,grade为第三个 //获得子节点的各项值,在给行数据赋值 DataRow dr = dt.NewRow(); XmlNode xx= x; string name = x.ChildNodes[0].InnerXml; dr["书名"] = name; string auther = x.ChildNodes[1].InnerXml; dr["作者"] = auther; string grade = x.ChildNodes[2].InnerXml; dr["等级"] = grade;
dt.Rows.Add(dr);//添加行 }
//创建DataSet对象,绑定数据 DataSet ds = new DataSet(); ds.Tables.Add(dt); dataGridView1.DataSource = dt;
} 

/// <summary>

/// 创建try.xml文件

/// </summary>

private static void CreateXml()
{
XmlDocument doc
= new XmlDocument();
XmlDeclaration dec
= doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
XmlElement books
= doc.CreateElement("books");
doc.AppendChild(books);

XmlElement book1
= doc.CreateElement("book");
books.AppendChild(book1);
XmlElement name1
= doc.CreateElement("name");
name1.InnerXml
= "语文教材";
book1.AppendChild(name1);
XmlElement auther1
= doc.CreateElement("auther");
auther1.InnerXml
= "文者";
book1.AppendChild(auther1);
XmlElement grade1
= doc.CreateElement("grade");
grade1.InnerXml
= "语级";
book1.AppendChild(grade1);

XmlElement book2
= doc.CreateElement("book");
books.AppendChild(book2);
XmlElement name2
= doc.CreateElement("name");
name2.InnerXml
= "数学教材";
book2.AppendChild(name2);
XmlElement auther2
= doc.CreateElement("auther");
auther2.InnerXml
= "数者";
book2.AppendChild(auther2);
XmlElement grade2
= doc.CreateElement("grade");
grade2.InnerXml
= "数级";
book2.AppendChild(grade2);

XmlElement book3
= doc.CreateElement("book");
books.AppendChild(book3);
XmlElement name3
= doc.CreateElement("name");
name3.InnerXml
= "英语教材";
book3.AppendChild(name3);
XmlElement auther3
= doc.CreateElement("auther");
auther3.InnerXml
= "英者";
book3.AppendChild(auther3);
XmlElement grade3
= doc.CreateElement("grade");
grade3.InnerXml
= "英语级";
book3.AppendChild(grade3);

doc.Save(Application.StartupPath
+ @"\try.xml");

//MessageBox.Show("创建try.xml成功");
}
}
}

第二种 获取数据更为简单

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;
using System.Xml.Linq;

namespace WindowsFormsApp13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CreateXml();


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //没有列名的方法
            //DataSet ds = new DataSet();
            //ds.ReadXml("try.xml");
            //this.dataGridView1.DataSource = ds.Tables[0];

            //含有列名的方法
            //中心思想就是创建DataSet对象,给DataSet对象的table添加DataTable对象,给DataTable对象添加列名,在添加DataRow对象,查找获得xml文件的节点值,给DataRow循环赋值
            DataTable dt = new DataTable();
            //添加列名
            dt.Columns.Add("书名", typeof(string));
            dt.Columns.Add("作者", typeof(string));
            dt.Columns.Add("等级", typeof(string));
            //XmlDocument doc= new XmlDocument();
            //doc.Load("try.xml");

            //加载xml文件
            XElement dd = XElement.Load("try.xml");
            //获取所有book节点的集合
            var node = from n in dd.Elements("book") select n;
            foreach (var n in node)
            {
                DataRow dr = dt.NewRow();
                dr["书名"] = n.Element("name").Value;
                dr["作者"] = n.Element("auther").Value;
                dr["等级"] = n.Element("grade").Value;
                dt.Rows.Add(dr);

            }

            //创建DataSet对象,绑定数据
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            dataGridView1.DataSource = dt;     
//dataGridView1.DataSource=dt.tables[0]; }
/// <summary> /// 创建try.xml文件 /// </summary> private static void CreateXml() { XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); XmlElement books = doc.CreateElement("books"); doc.AppendChild(books); XmlElement book1 = doc.CreateElement("book"); books.AppendChild(book1); XmlElement name1 = doc.CreateElement("name"); name1.InnerXml = "语文教材"; book1.AppendChild(name1); XmlElement auther1 = doc.CreateElement("auther"); auther1.InnerXml = "文者"; book1.AppendChild(auther1); XmlElement grade1 = doc.CreateElement("grade"); grade1.InnerXml = "语级"; book1.AppendChild(grade1); XmlElement book2 = doc.CreateElement("book"); books.AppendChild(book2); XmlElement name2 = doc.CreateElement("name"); name2.InnerXml = "数学教材"; book2.AppendChild(name2); XmlElement auther2 = doc.CreateElement("auther"); auther2.InnerXml = "数者"; book2.AppendChild(auther2); XmlElement grade2 = doc.CreateElement("grade"); grade2.InnerXml = "数级"; book2.AppendChild(grade2); XmlElement book3 = doc.CreateElement("book"); books.AppendChild(book3); XmlElement name3 = doc.CreateElement("name"); name3.InnerXml = "英语教材"; book3.AppendChild(name3); XmlElement auther3 = doc.CreateElement("auther"); auther3.InnerXml = "英者"; book3.AppendChild(auther3); XmlElement grade3 = doc.CreateElement("grade"); grade3.InnerXml = "英语级"; book3.AppendChild(grade3); doc.Save(Application.StartupPath + @"\try.xml"); //MessageBox.Show("创建try.xml成功"); } } }

 

posted @ 2017-04-20 10:50  冲天小肥牛  阅读(1524)  评论(0编辑  收藏  举报