XmlDocument xml; string path=Application.StartupPath + "\\功能测试参数.xml"; if(System.IO.File.Exists(path)){ xml = new XmlDocument(); doc.Load(path); //加载XML文档 } else { xml = new XmlDocument(); //创建根节点 config xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", "")); XmlElement one = xml.CreateElement("功能测试参数"); //把根节点加到xml文档中 xml.AppendChild(one); } XmlElement two = xml.CreateElement(comboBox1.Text); one.AppendChild(two); XmlElement three = xml.CreateElement("测试项目"); foreach (string item in listBox1.Items) //每次需要保存的东西 { string str = "项目" + i.ToString(); i++; three.SetAttribute(str, item); //str=item } two.AppendChild(three); xml.Save(path);