C# 創建、載入XML文件學習筆記

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);
                    }




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章