C# 创建、载入XML文件学习笔记

来源:互联网 发布:网络销售渠道有哪些 编辑:程序博客网 时间:2024/05/17 00:59

1、C#创建XML

(1)创建XML文档对象

XmlDocument doc = new XmlDocument();

(2)创建第一个行描述信息,并且添加到doc文档中

XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);  doc.AppendChild(dec);

(3)添加一级节点,创建根节点,将根节点添加到文档中

XmlElement stations = doc.CreateElement("Stations");                 doc.AppendChild(stations);

(4)添加二级节点,给根节点Stations创建子节点,将station添加到根节点

XmlElement station = doc.CreateElement("Station");                     stations.AppendChild(station);

(5)添加三级节点,建立元素并添加到二级节点
        XmlElement id = doc.CreateElement("ID");                    id.InnerText = (i + 1).ToString();                    station.AppendChild(id);


public void dgvtoxml(string path)

        {
            try
            {
                //新建XML文件
                XmlDocument doc = new XmlDocument();//1、创建XML文档对象
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); //2、创建第一个行描述信息,并且添加到doc文档中
                doc.AppendChild(dec);
                XmlElement stations = doc.CreateElement("Stations");//3、创建根节点,将根节点添加到文档中
                doc.AppendChild(stations);


                int row = dataGridView_canshu.Rows.Count ;//得到总行数
                for (int i = 0; i < row; i++)
                {
                    XmlElement station = doc.CreateElement("Station");//4、给根节点Stations创建子节点,将station添加到根节点
                    stations.AppendChild(station);

                    //5、添加ID子元素
                    XmlElement id = doc.CreateElement("ID");
                    id.InnerText = (i + 1).ToString();
                    station.AppendChild(id);

                    //5、添加Name子节点
                    XmlElement name = doc.CreateElement("Name");
                    name.InnerText = dataGridView_canshu.Rows[i].Cells[1].Value.ToString();
                    station.AppendChild(name);
                }

                doc.Save(path);
                MessageBox.Show("数据保存成功!");
            }
            catch 
            {
                MessageBox.Show("数据行存在空值,请进行核对!!!");
            }
        }


2、载入XML文件

(1)创建XML文档对象

XmlDocument doc = new XmlDocument();

(2)获取文件路径 FileName

(3)载Xml文件 doc.Load(opdialog.FileName);

(4)获取根节点XmlElement rootElem = doc.DocumentElement;  

(5)遍历二级节点 下的三级节点元素(遍历二级节点下的子节点并将其进行保存)

foreach (XmlNode node in rootElem.ChildNodes)//遍历二级节点
                    {
                        string id = ""; string name = "";
                        string traveltime = ""; string stoptime = "";
                        string dtime = ""; string ftime = "";
                        foreach (XmlNode node1 in node.ChildNodes)//遍历三级节点
                        {
                            switch (node1.Name)
                            {
                                case "ID": id = node1.InnerText; break;
                                case "
Name": name = node1.InnerText; break;
                            }
                        }
                        //新建行
添加到datagridview的行数据
                        dgv.Rows.Add(id, name, traveltime, stoptime, dtime, ftime);
                    }




0 0
原创粉丝点击