将数据写入XML配置文件中

 

创建一个XML文件

        /// <summary>
        /// 创建xml
        /// </summary>
        public void createXMl()
        {
           //设置配置文件的路径
            XmlTextWriter myXmlTextWriter = new XmlTextWriter(@"..\..\Config\Config.xml", null);
            //使用 Formatting 属性指定希望将 XML 设定为何种格式。 
            //子元素就可以通过 Indentation 和 IndentChar 属性来缩进。
            myXmlTextWriter.Formatting = Formatting.Indented;
            myXmlTextWriter.WriteStartDocument(false);
            myXmlTextWriter.WriteStartElement("store");
            myXmlTextWriter.WriteComment("记录信息");//xml中备注
            myXmlTextWriter.WriteStartElement("item");
            //设置节点的属性
            myXmlTextWriter.WriteAttributeString("Type", "类型");
            myXmlTextWriter.WriteAttributeString("Time", "2015-12-1");


            //设置子节点
            myXmlTextWriter.WriteElementString("code", "123");
            myXmlTextWriter.WriteElementString("name", "名称");
            myXmlTextWriter.WriteElementString("price", "33.0");



            myXmlTextWriter.WriteEndElement();
            myXmlTextWriter.Flush();
            myXmlTextWriter.Close();

        }    

向配置文件中填写数据

 /// <summary>
        /// 给XML添加数据
        /// </summary>
        public void AddXML()
        {
            ///加载文件并选出要结点:
            XmlDocument doc = new XmlDocument();
            doc.Load(@"..\..\Config\Config.xml");
            XmlNode root = doc.SelectSingleNode("store");

            //创建一个结点
            XmlElement xelKey = doc.CreateElement("item");
            //设置结点的属性:
            //XmlAttribute xelType = doc.CreateAttribute("Type");
            //xelType.InnerText = "adfdsf";
            //xelKey.SetAttributeNode(xelType);
            //创建子结点
            XmlElement xelAuthor1 = doc.CreateElement("code");
            xelAuthor1.InnerText = "销售商代码7";
            XmlElement xelAuthor2 = doc.CreateElement("name");
            xelAuthor2.InnerText = "销售商名字7";
            XmlElement xelAuthor3 = doc.CreateElement("price");
            xelAuthor3.InnerText = "邮箱7";
         

            xelKey.AppendChild(xelAuthor1);
            xelKey.AppendChild(xelAuthor2);
            xelKey.AppendChild(xelAuthor3);
        
            // 最后把book结点挂接在要结点上,并保存整个文件:
            root.AppendChild(xelKey);
            doc.Save(@"..\..\Config\Config.xml");
        }

读取出已有的XML数据并循环遍历转换成泛型集合返回出去

 /// <summary>
        /// 获取xml文件中的数据
        /// </summary>
        public List<xmlModel> GetXmlInfo()
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;//忽略文档里面的注释
            XmlReader reader = XmlReader.Create(@"..\..\Config\Config.xml", settings);
            xmlDoc.Load(reader);
            XmlNode xn = xmlDoc.SelectSingleNode("NewDataSet");
            XmlNodeList xnl = xn.ChildNodes;
            List<xmlModel> xmlModelList = new List<xmlModel>();
            foreach (XmlNode xn1 in xnl)
            {
                xmlModel xmlModel = new xmlModel();
                // 将节点转换为元素,便于得到节点的属性值
                XmlElement xe = (XmlElement)xn1;
                // 得到属性的属性值
                //XmlAttribute xelType = doc.CreateAttribute("Type");
                //xelType.InnerText = "adfdsf";
                // 得到itme节点的所有子节点
                XmlNodeList xnl0 = xe.ChildNodes;
                xmlModel.SaleCode = xnl0.Item(0).InnerText;
                xmlModel.SaleName = xnl0.Item(1).InnerText;
                xmlModel.Email = xnl0.Item(2).InnerText;
                xmlModel.UserMoblic = xnl0.Item(3).InnerText;
            
                xmlModelList.Add(xmlModel);
                reader.Close();
            }

            return xmlModelList;
        }

 

你可能感兴趣的:(将数据写入XML配置文件中)