C# 如何在WINForm程序中創建XML文件

<?xml version="1.0" encoding="gb2312"?>
<FilesInformation>
  <version>1.0.1818.42821</version>
  <description>說明</description>
  <FileItem 
  FileName="name"
  FileVersion="sdf"
  FileLength="sdf"
  FileCreationTime="sd"
  />
</FilesInformation>
string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;   

獲取和設置包含該應用程序的目錄的名稱

File.Exists(path + XmlFileName) 

File.Exists是判斷文件是否存在,傳入參數為路徑+文件名

XmlDocument xmlDoc = new XmlDocument();    

這一句是創建一個XmlDocument對象

XmlDeclaration xmlSM = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);   

這一句是添加xml文件頭的聲明

xmlDoc.AppendChild(xmlSM); 

這一句是將創建的XmlDocument對象追加到xml文件聲明後面

XmlElement DeviceTree = xmlDoc.CreateElement("DeviceTree"); 

這一句為創建一個標簽名為DeviceTree的節點

DeviceTree.SetAttribute("name", "設備樹");

這一句設置節點的name屬性為設備樹

xmlDoc.AppendChild(DeviceTree);

這一句是將創建的節點添加到開始創建的XmlDocument對象中

xmlDoc.Save(path + XmlFileName);

最後是保存創建好的xml文件

方法1:

private void button1_Click(object sender, EventArgs e) 
{     
XmlDocument xmlDoc = new XmlDocument();           //建立Xml的定義聲明        
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);        
xmlDoc.AppendChild(dec);           //創建根節點        
XmlElement root = xmlDoc.CreateElement("FilesInformation");        
xmlDoc.AppendChild(root);       
XmlElement version = xmlDoc.CreateElement("version");      version.InnerText = "1.0.1818.42821";     
root.AppendChild(version);         
XmlElement description = xmlDoc.CreateElement("description");     
description.InnerText = "說明";     
root.AppendChild(description);       
XmlElement fileItem = xmlDoc.CreateElement("FileItem");     
fileItem.SetAttribute("FileName", "name");     
fileItem.SetAttribute("FileVersion", "xx");     
fileItem.SetAttribute("FileLength", "xxx");     
fileItem.SetAttribute("FileCreationTime", "xxxx");     
root.AppendChild(fileItem);          
xmlDoc.Save("test.xml");   
 }

方法2:

XmlDocument xmldoc = new XmlDocument();
               XmlText xmltext;
 
               //聲明
               XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
               xmlnode.InnerText += " encoding=\"GB2312\"";
               xmldoc.AppendChild(xmlnode);
 
               //添加根節點
               XmlElement xmlelementroot = xmldoc.CreateElement("", "Config", "");
               //根節點包含節點文本時會造成XML文檔結構的混亂
               //xmltext = xmldoc.CreateTextNode("配置信息");
               //xmlelementroot.AppendChild(xmltext);
               xmldoc.AppendChild(xmlelementroot);
 
               //添加一個元素
               XmlElement xmlelement1 = xmldoc.CreateElement("", "DTL", "");
               xmltext = xmldoc.CreateTextNode("2010-10-25");
               xmlelement1.AppendChild(xmltext);
               xmldoc.ChildNodes.Item(1).AppendChild(xmlelement1);
 
               //添加另一個元素
               XmlElement xmlelement2 = xmldoc.CreateElement("", "DTF", "");
               xmltext = xmldoc.CreateTextNode("2011-02-10");
               xmlelement2.AppendChild(xmltext);
               xmldoc.ChildNodes.Item(1).AppendChild(xmlelement2);
 
               //保存
               xmldoc.Save(Environment.CurrentDirectory+\\111.xml);

方法3:

XmlTextWriter xmlwriter = new XmlTextWriter(getPath(), Encoding.Default);
                xmlwriter.Formatting = Formatting.Indented;
                xmlwriter.Indentation = 4;
 
                xmlwriter.WriteStartDocument();
                xmlwriter.WriteStartElement("", "Config", "");
 
                xmlwriter.WriteStartElement("", "DTL", "");
                xmlwriter.WriteString("2010-10-25");
                xmlwriter.WriteEndElement();
 
                xmlwriter.WriteStartElement("", "DTF", "");
                xmlwriter.WriteString("2011-02-10");
                xmlwriter.WriteEndElement();
 
                xmlwriter.WriteEndElement();
                xmlwriter.WriteEndDocument();
 
                xmlwriter.Flush();
                xmlwriter.Close();

上面代碼中的getPath()是自定義的一個獲取文件路徑加名稱的方法,請根據自己實際情況修改!我一般設定為

Environment.CurrentDirectory+\\111.xml

總的來說還是方法三比較容易理解,簡單易用,也是我常用的方法!

希望對各位有所幫助!

以上就是C# 如何在WINForm程序中創建XML文件的詳細內容,更多關於c# 創建XML文件的資料請關註WalkonNet其它相關文章!

推薦閱讀: