<?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; node
獲取和設置包含該應用程序的目錄的名稱微信
File.Exists(path + XmlFileName) orm
File.Exists是判斷文件是否存在,傳入參數爲路徑+文件名xml
XmlDocument xmlDoc = new XmlDocument(); 對象
這一句是建立一個XmlDocument對象blog
XmlDeclaration xmlSM = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); ip
這一句是添加xml文件頭的聲明utf-8
xmlDoc.AppendChild(xmlSM); element
這一句是將建立的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,切記備註 加羣,小編將會第一時間邀請你進羣!