`

VS C# 2008导入到xml

 
阅读更多

//创建MobileBackup.xml文件
        private void createXml()
        {
            //创建一个XmlTextWriter实例
            XmlTextWriter xmlTW = new XmlTextWriter(path + @"\MobileBackup.xml", Encoding.UTF8);

            //开始写
            xmlTW.WriteStartDocument();
            xmlTW.WriteStartElement("SHT_MOBILE");

            //结束
            xmlTW.WriteEndElement();
            xmlTW.WriteEndDocument();
            xmlTW.Close();
        }

        //创建一个XmlDocument对象
        private XmlDocument xmlDo = null;

        //清空xml文件里的所有节点
        private void clearXmlNode()
        {//创建一个XmlDocument实例
            if(xmlDo == null)
                xmlDo = new XmlDocument();
            xmlDo.Load(path + @"\MobileBackup.xml");
           
            if (File.Exists(path + @"\MobileBackup.xml"))
            {
                //清空xml文件里的所有节点
                XmlNodeList xmlNl = xmlDo.SelectSingleNode("SHT_MOBILE").ChildNodes;
                foreach (XmlNode xmlN in xmlNl)
                {
                    XmlElement xmlEl = (XmlElement)xmlN;
                    xmlEl.RemoveAll();

                    if (xmlEl.ChildNodes.Count <= 0)
                    {
                        xmlN.ParentNode.RemoveChild(xmlN);
                    }
                }
                //保存文件
                xmlDo.Save(path + @"\MobileBackup.xml");
            }
        }
       
        //导入发件箱xml节点
        private void insertToXmlOutBox()
        {
            if(xmlDo == null)
                xmlDo = new XmlDocument();
            xmlDo.Load(path + @"\MobileBackup.xml");//加载xml文件

            XmlNode rootNode = null;
            XmlElement xmlE_OutBox = null, xmlE_Mobile = null, xmlE_Message = null, xmlE_Time = null ;

            //找到根节点
            rootNode = xmlDo.SelectSingleNode("SHT_MOBILE");

            for (int i = 0; i < this.sHToutBoxDataGridView.Rows.Count; i++)
            {

                //添加项节点SHToutBox
                xmlE_OutBox = xmlDo.CreateElement("SHToutBox");
                xmlE_OutBox.SetAttribute("flag", "SHToutBox");

                //添加mobile节点
                xmlE_Mobile = xmlDo.CreateElement("mobile");
                xmlE_Mobile.InnerText = this.sHToutBoxDataGridView[0, i].Value.ToString();
                xmlE_OutBox.AppendChild(xmlE_Mobile);

                //添加message节点
                xmlE_Message = xmlDo.CreateElement("message");
                xmlE_Message.InnerText = this.sHToutBoxDataGridView[1, i].Value.ToString();
                xmlE_OutBox.AppendChild(xmlE_Message);

                //添加time节点
                xmlE_Time = xmlDo.CreateElement("time");
                xmlE_Time.InnerText = this.sHToutBoxDataGridView[2, i].Value.ToString();
                xmlE_OutBox.AppendChild(xmlE_Time);

                //把所有节点添加到rootNode上
                rootNode.AppendChild(xmlE_OutBox);
                //保存
                xmlDo.Save(path + @"\MobileBackup.xml");
            }
        }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics