XmlDocument 操作XML --本文件是转载的。。在此感谢那位写文的前辈!

这是一篇不错的C#关于XmlDocument对象 关于操作XML文件的文件。本人从中受益非常!
今与大家共享之。。。
读写XML文件 
     
<?xml version="1.0" encoding="utf-8"?>
<Employeers>
  <Employeer ID="DBT39435M">
    <HireDate>1/1/1990 12:00:00 上午</HireDate>
    <JobLvl>75</JobLvl>
    <JobID>11</JobID>
    <LastName>Tonini</LastName>
    <FirstName>Daniel</FirstName>
  </Employeer>
</Employeers>
以上是xml文件模板。

   /**//// <summary>
        /// 写入xml文件
        /// </summary>
        /// <param name="fileName">完整文件名</param>
        private void WriteXml(string fileName)
        {
            DataTable dt = DataAccess.AjaxData.GetXmlData();
            XmlDocument xmldoc = new XmlDocument();
            //创建文件头信息
            XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //创建文件根节点
            XmlElement rootNode = xmldoc.CreateElement("Employeers");
            xmldoc.InsertBefore(xmldec, xmldoc.DocumentElement);
            xmldoc.AppendChild(rootNode);

            //遍历数据表,
            foreach (DataRow dr in dt.Rows)
            {
                //创建一条记录的父节点
                XmlElement parentNode = xmldoc.CreateElement("Employeer");
                parentNode.SetAttribute("ID", dr["emp_id"].ToString());
                xmldoc.DocumentElement.PrependChild(parentNode);
                //为每个字段创建一个子节点
                XmlElement FName = xmldoc.CreateElement("FirstName");
                XmlElement LName = xmldoc.CreateElement("LastName");
                XmlElement JobID = xmldoc.CreateElement("JobID");
                XmlElement JobLvl = xmldoc.CreateElement("JobLvl");
                XmlElement HireDate = xmldoc.CreateElement("HireDate");
               
                //创建子节点的文字描述
                XmlText fnametext = xmldoc.CreateTextNode(dr["fname"].ToString());
                XmlText lnametext = xmldoc.CreateTextNode(dr["lname"].ToString());
                XmlText jobidtext = xmldoc.CreateTextNode(dr["job_id"].ToString());
                XmlText joblvtextl = xmldoc.CreateTextNode(dr["job_lvl"].ToString());
                XmlText hiredatetext = xmldoc.CreateTextNode(dr["hire_date"].ToString());

                //将文字描述加到子节点
                FName.AppendChild(fnametext);
                LName.AppendChild(lnametext);
                JobID.AppendChild(jobidtext);
                JobLvl.AppendChild(joblvtextl);
                HireDate.AppendChild(hiredatetext);

                /**//*上面两段也可以通过这段来代替
                FName.InnerText = dr["fname"].ToString();
                LName.InnerText = dr["lname"].ToString();
                JobID.InnerText = dr["job_id"].ToString();
                JobLvl.InnerText = dr["job_lvl"].ToString();
                HireDate.InnerText = dr["hire_date"].ToString();
                */

                //将子节点加到父节点
                parentNode.PrependChild(FName);
                parentNode.PrependChild(LName);
                parentNode.PrependChild(JobID);
                parentNode.PrependChild(JobLvl);
                parentNode.PrependChild(HireDate);

            }

            //保存成xml文件
            xmldoc.Save(fileName);
        }

        /**//// <summary>
        /// 添加一个节点
        /// </summary>
        /// <param name="fileName"></param>
        public void AddNewNode(string fileName)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(fileName);

            //找到根节点
            XmlNode rootNode = xmldoc.SelectSingleNode("Employeers");

            //找到子节点
            XmlElement parentNode = xmldoc.CreateElement("Employeer");
            parentNode.SetAttribute("ID", "EMPID");

            //创建Employeer节点下的子节点
            XmlElement FName = xmldoc.CreateElement("FirstName");
            XmlElement LName = xmldoc.CreateElement("LastName");
            XmlElement JobID = xmldoc.CreateElement("JobID");
            XmlElement JobLvl = xmldoc.CreateElement("JobLvl");
            XmlElement HireDate = xmldoc.CreateElement("HireDate");

            FName.InnerText = "FName";
            LName.InnerText = "LName";
            JobID.InnerText = "JOBID";
            JobLvl.InnerText = "JOBLVL";
            HireDate.InnerText = "HIREDATE";

            parentNode.PrependChild(FName);
            parentNode.PrependChild(LName);
            parentNode.PrependChild(JobID);
            parentNode.PrependChild(JobLvl);
            parentNode.PrependChild(HireDate);

            //将新创建的Employeer子节点点加入到根节点。
            rootNode.AppendChild(parentNode);

            xmldoc.Save(fileName);
        }

        /**//// <summary>
        /// 修改节点
        /// </summary>
        /// <param name="filename"></param>
        private void UpdateNode(string filename)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(filename);
            //得到根节点的所有子节点
            XmlNodeList nodeList = xmldoc.SelectSingleNode("Employeers").ChildNodes;
            foreach (XmlNode node in nodeList)
            {
                XmlElement xe = (XmlElement)node;//强制转换成XmlElement类型。
                if (xe.GetAttribute("ID") == "001")
                {
                    XmlNodeList xnodeList = xe.ChildNodes;
                    foreach (XmlNode xnode in xnodeList)
                    {
                        XmlElement xex = (XmlElement)xnode;
                        if (xex.Name == "FirstName")
                        {
                            xex.InnerText = xex.InnerText + "Test";
                        }
                    }
                }
            }
            xmldoc.Save(filename);
        }

        /**//// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="filename"></param>
        public void DeleteNode(string filename)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(filename);
            XmlNodeList nodelist = xmldoc.SelectSingleNode("Employeers").ChildNodes;
            foreach (XmlNode node in nodelist)
            {
                XmlElement xe = (XmlElement)node;//强制转换成XmlElement类型。
                //xe.RemoveAttribute("ID");删除节点的ID属性。
                if (xe.GetAttribute("ID") == "001")
                {
                    XmlNodeList xnodeList = xe.ChildNodes;
                    foreach (XmlNode xnode in xnodeList)
                    {
                        XmlElement xex = (XmlElement)xnode;
                        if (xex.Name == "FirstName")
                        {
                            xe.RemoveChild(xex);//删除一个节点
                        }
                    }
                }
            }
            xmldoc.Save(filename);
        }

读取上面保存的xml文件就容易多了直接调用DataSet的ReadXml方法就可以。
            DataSet ds = new DataSet();
            ds.ReadXml(@"c:\temp\emp.xml");

posted @ 2008-03-14 13:59  以函  阅读(546)  评论(0编辑  收藏  举报