如何用C#動態創建XML文件並保存至指定路徑


     using System.Xml;  //導入命名空間    

     ///
<summary> /// 使用C#創建一個XML文件 /// </summary> public void CreateXML() { XmlDocument doc = new XmlDocument(); //創建第一行的描述信息 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); //添加到文檔中 //給文檔添加根節點 並將根節點添加到文檔對象 XmlElement typeB = doc.CreateElement("typeB"); typeB.SetAttribute("version", "1.0"); //設置節點的屬性和屬性值 doc.AppendChild(typeB); //將根節點添加到文檔中 //創建子節點 XmlElement programList = doc.CreateElement("ProgramList"); //創建子節點programList typeB.AppendChild(programList); //將創建的子節點添加到typeB根節點下 XmlElement playTime = doc.CreateElement("PlayTime"); //創建一個子節點名為playTime playTime.InnerXml = "<a>2018</a>"; //為這個子節點添加標簽以及內容 programList.AppendChild(playTime); //將這個子節點添加到programList節點下 //保存文件 doc.Save("鳳凰衛視.xml"); //可以指定保存路徑,我這里直接寫會默認保存在項目Bin\DeBug文件夾下 MessageBox.Show("保存成功!"); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM