C# 如何在WINForm程序中創(chuàng)建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;
獲取和設(shè)置包含該應(yīng)用程序的目錄的名稱
File.Exists(path + XmlFileName)
File.Exists是判斷文件是否存在,傳入?yún)?shù)為路徑+文件名
XmlDocument xmlDoc = new XmlDocument();
這一句是創(chuàng)建一個XmlDocument對象
XmlDeclaration xmlSM = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
這一句是添加xml文件頭的聲明
xmlDoc.AppendChild(xmlSM);
這一句是將創(chuàng)建的XmlDocument對象追加到xml文件聲明后面
XmlElement DeviceTree = xmlDoc.CreateElement("DeviceTree");
這一句為創(chuàng)建一個標(biāo)簽名為DeviceTree的節(jié)點
DeviceTree.SetAttribute("name", "設(shè)備樹");
這一句設(shè)置節(jié)點的name屬性為設(shè)備樹
xmlDoc.AppendChild(DeviceTree);
這一句是將創(chuàng)建的節(jié)點添加到開始創(chuàng)建的XmlDocument對象中
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é)點
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);
//添加根節(jié)點
XmlElement xmlelementroot = xmldoc.CreateElement("", "Config", "");
//根節(jié)點包含節(jié)點文本時會造成XML文檔結(jié)構(gòu)的混亂
//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()是自定義的一個獲取文件路徑加名稱的方法,請根據(jù)自己實際情況修改!我一般設(shè)定為
Environment.CurrentDirectory+\\111.xml
總的來說還是方法三比較容易理解,簡單易用,也是我常用的方法!
希望對各位有所幫助!
以上就是C# 如何在WINForm程序中創(chuàng)建XML文件的詳細內(nèi)容,更多關(guān)于c# 創(chuàng)建XML文件的資料請關(guān)注腳本之家其它相關(guān)文章!
- 使用Visual Studio2019創(chuàng)建C#項目(窗體應(yīng)用程序、控制臺應(yīng)用程序、Web應(yīng)用程序)
- C#創(chuàng)建SQLite控制臺應(yīng)用程序詳解
- C#創(chuàng)建WCF服務(wù)控制臺應(yīng)用程序詳解
- C#創(chuàng)建Web應(yīng)用程序代碼實例
- C#通過創(chuàng)建Windows服務(wù)啟動程序的方法詳解
- C#程序中創(chuàng)建、復(fù)制、移動、刪除文件或文件夾的示例
- C#操作IIS程序池及站點的創(chuàng)建配置實現(xiàn)代碼
- C# 創(chuàng)建控制臺應(yīng)用程序
相關(guān)文章
c#實現(xiàn)幾種數(shù)據(jù)庫的大數(shù)據(jù)批量插入
這篇文章主要介紹了c#實現(xiàn)幾種數(shù)據(jù)庫的大數(shù)據(jù)批量插入,主要包括SqlServer、Oracle、SQLite和MySQL,有興趣的可以了解一下。2017-01-01
C#使用DllImport調(diào)用非托管的代碼的方法
C#調(diào)用非托管代碼的方式主要有Com調(diào)用、DllImport方式調(diào)用、加載非托管動態(tài)鏈接庫、直接執(zhí)行機器碼等方式?,F(xiàn)在介紹一下我自己常用的DllImport方式調(diào)用MSDN中提到的GetShortPathName方法;2013-03-03

