读取xml文件及修改子节点内容

 

System.Xml.XmlDocument doc1 = new System.Xml.XmlDocument();
            //获得配置文件的全路径  
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "DbUserAccount.xml";
            //加载
            doc1.Load(strFileName);
            //找出名称为“add”的所有元素   
            System.Xml.XmlNodeList nodes = doc1.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                //获得将当前元素的key属性   
                System.Xml.XmlAttribute _key = nodes[i].Attributes["key"];
                if (_key != null)
                {
                    //根据元素的第一个属性来判断当前的元素是不是目标元素   param为数据集合
                    foreach (var item in param)
                    {
                        if (_key.Value == item.Key)
                        {
                            //对目标元素中的第二个属性赋值
                            _key = nodes[i].Attributes["value"];
                            _key.Value = item.Value.ToString();
                        }
                    }
                }
            }

posted @ 2013-11-25 11:15  袁半仙  阅读(482)  评论(0)    收藏  举报