1. 程式人生 > >C# 將資料儲存進xml檔案

C# 將資料儲存進xml檔案

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);