<?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#知識,請掃描下方公眾號二維碼

需加微信交流群的,請加小編微信號z438679770,切記備注 加群,小編將會第一時間邀請你進群!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/262753.html
標籤:WinForm
上一篇:WPF之事件
