欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 如何在WINForm程序中創(chuàng)建XML文件

 更新時(shí)間:2021年02月25日 15:29:08   作者:zls366  
這篇文章主要介紹了C# 如何在WINForm程序中創(chuàng)建XML文件,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
<?xml version="1.0" encoding="gb2312"?>
<FilesInformation>
  <version>1.0.1818.42821</version>
  <description>說(shuō)明</description>
  <FileItem 
  FileName="name"
  FileVersion="sdf"
  FileLength="sdf"
  FileCreationTime="sd"
  />
</FilesInformation>
string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;   

獲取和設(shè)置包含該應(yīng)用程序的目錄的名稱

File.Exists(path + XmlFileName) 

File.Exists是判斷文件是否存在,傳入?yún)?shù)為路徑+文件名

XmlDocument xmlDoc = new XmlDocument();    

這一句是創(chuàng)建一個(gè)XmlDocument對(duì)象

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

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

xmlDoc.AppendChild(xmlSM); 

這一句是將創(chuàng)建的XmlDocument對(duì)象追加到xml文件聲明后面

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

這一句為創(chuàng)建一個(gè)標(biāo)簽名為DeviceTree的節(jié)點(diǎn)

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

這一句設(shè)置節(jié)點(diǎn)的name屬性為設(shè)備樹

xmlDoc.AppendChild(DeviceTree);

這一句是將創(chuàng)建的節(jié)點(diǎn)添加到開始創(chuàng)建的XmlDocument對(duì)象中

xmlDoc.Save(path + XmlFileName);

最后是保存創(chuàng)建好的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);           //創(chuàng)建根節(jié)點(diǎn)        
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 = "說(shuō)明";     
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);
 
               //添加根節(jié)點(diǎn)
               XmlElement xmlelementroot = xmldoc.CreateElement("", "Config", "");
               //根節(jié)點(diǎn)包含節(jié)點(diǎn)文本時(shí)會(huì)造成XML文檔結(jié)構(gòu)的混亂
               //xmltext = xmldoc.CreateTextNode("配置信息");
               //xmlelementroot.AppendChild(xmltext);
               xmldoc.AppendChild(xmlelementroot);
 
               //添加一個(gè)元素
               XmlElement xmlelement1 = xmldoc.CreateElement("", "DTL", "");
               xmltext = xmldoc.CreateTextNode("2010-10-25");
               xmlelement1.AppendChild(xmltext);
               xmldoc.ChildNodes.Item(1).AppendChild(xmlelement1);
 
               //添加另一個(gè)元素
               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()是自定義的一個(gè)獲取文件路徑加名稱的方法,請(qǐng)根據(jù)自己實(shí)際情況修改!我一般設(shè)定為

Environment.CurrentDirectory+\\111.xml

總的來(lái)說(shuō)還是方法三比較容易理解,簡(jiǎn)單易用,也是我常用的方法!

希望對(duì)各位有所幫助!

以上就是C# 如何在WINForm程序中創(chuàng)建XML文件的詳細(xì)內(nèi)容,更多關(guān)于c# 創(chuàng)建XML文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論