最近工作中遇到一個(gè)問題,要求創(chuàng)建一個(gè)xml文件,在創(chuàng)建的時(shí)候要初始化該XML文檔,同時(shí)該文檔打開后是XML形式,但是后綴名不是。在網(wǎng)上找了好些資料沒找到,只能自己試著弄了一下,沒想到成功了,把它記下來作為自己的學(xué)習(xí)筆記。
需求:創(chuàng)建XML文件,后綴名為.xwsp
初始化的文檔節(jié)點(diǎn)如下:

1 <?xml version="1.0" encoding="UTF-8"?> 2 <xxxversion="1.0" name="aaa"> 3 <CreationInfo> 4 <CreatedBy>CreateUser</CreatedBy> 5 <CreatedTime>2015/10/1 14:03:48</CreatedTime> 6 <SavedTime>2015/10/1 14:03:48</SavedTime> 7 </CreationInfo> 8 <a/> 9 <b/>10 <c/>11 </xxx>
首先第一個(gè)問題:后綴名為.xwsp,打開后顯示的XML文本
當(dāng)時(shí)這個(gè)問題想復(fù)雜了,因?yàn)橐M(jìn)行二進(jìn)制轉(zhuǎn)換之類的,網(wǎng)上找了老半天沒找到,最后自己試了一下,簡單的要死,只能說自己笨
解決方法:xmlDoc.Save("a.xwsp");
只要保存xml文件的時(shí)候改了后綴名即可,我也是醉了
第二個(gè)問題:添加節(jié)點(diǎn)的時(shí)候尤其是添加<CreatedBy><CreatedTime><SavedTime>這三個(gè)節(jié)點(diǎn)的時(shí)候老是添加不進(jìn)去
當(dāng)時(shí)寫的代碼如下:

1 PRivate static void CreateXwspFile(string fileName, string path) 2 { 3 XmlDocument xmlDoc = new XmlDocument(); 4 //創(chuàng)建類型聲明節(jié)點(diǎn) 5 XmlDeclaration xdDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); 6 xmlDoc.AppendChild(xdDec); 7 8 //創(chuàng)建根節(jié)點(diǎn) 9 XmlElement xeRoot = xmlDoc.CreateElement("xxx");10 //給節(jié)點(diǎn)屬性賦值11 xeRoot.SetAttribute("version", "1.0");12 xeRoot.SetAttribute("name", fileName);13 xmlDoc.AppendChild(xeRoot);14 15 //創(chuàng)建并添加<CreationInfo></CreationInfo>節(jié)點(diǎn)16 xeRoot = xmlDoc.CreateElement("CreationInfo");17 XmlNode xnXwsp = xmlDoc.SelectSingleNode("xxx");18 if (xnXwsp != null)19 {20 xnXwsp.AppendChild(xeRoot);21 }22 23 //創(chuàng)建并添加<CreatedBy></CreatedBy>節(jié)點(diǎn)24 xeRoot = xmlDoc.CreateElement("CreatedBy");25 xnXwsp = xmlDoc.SelectSingleNode("CreationInfo");26 if (xnXwsp != null)27 {28 xnXwsp.AppendChild(xeRoot);29 }30 31 //創(chuàng)建并添加<CreatedTime></CreatedTime>節(jié)點(diǎn)32 xeRoot = xmlDoc.CreateElement("CreatedTime");33 xnXwsp = xmlDoc.SelectSingleNode("CreationInfo");34 if (xnXwsp != null)35 {36 xnXwsp.AppendChild(xeRoot);37 }38 39 //創(chuàng)建并添加<SavedTime></SavedTime>節(jié)點(diǎn)40 xeRoot = xmlDoc.CreateElement("SavedTime");41 xnXwsp = xmlDoc.SelectSingleNode("CreationInfo");42 if (xnXwsp != null)43 {44 xnXwsp.AppendChild(xeRoot);45 }46 47 //創(chuàng)建并添加<a></a>節(jié)點(diǎn)48 xeRoot = xmlDoc.CreateElement("a");49 xnXwsp = xmlDoc.SelectSingleNode("xxx");50 if (xnXwsp != null)51 {52 xnXwsp.AppendChild(xeRoot);53 }54 //創(chuàng)建并添加<b></b>節(jié)點(diǎn)55 xeRoot = xmlDoc.CreateElement("b");56 xnXwsp = xmlDoc.SelectSingleNode("xxx");57 if (xnXwsp != null)58 {59 xnXwsp.AppendChild(xeRoot);60 }61 //創(chuàng)建并添加<c></c>節(jié)點(diǎn)62 xeRoot = xmlDoc.CreateElement("c");63 xnXwsp = xmlDoc.SelectSingleNode("xxx");64 if (xnXwsp != null)65 {66 xnXwsp.AppendChild(xeRoot);67 }68 //保存XML文檔69 try70 {71 xmlDoc.Save(path + fileName + ".xwsp");72 }73 catch (Exception ep)74 {75 Console.WriteLine(ep.Message);76 }77 }
結(jié)果如下:

1 <?xml version="1.0" encoding="utf-8"?>2 <xxx version="1.0" name="workspace1">3 <CreationInfo />4 <a />5 <b />6 <c />7 </xxx>
子節(jié)點(diǎn)<CreatedBy><CreatedTime><SavedTime>死活出不來,打斷點(diǎn)<CreationInfo>節(jié)點(diǎn)先添加進(jìn)去了,但是xnXwsp = xmlDoc.SelectSingleNode("CreationInfo");這一句的結(jié)果死活為null,想不通,現(xiàn)在都沒想通,后來換了一種寫法就OK了
這種得不到正確寫法的思路是:先添加父節(jié)點(diǎn)<CreationInfo>再添加子節(jié)點(diǎn)<CreatedBy><CreatedTime><SavedTime>
網(wǎng)上找到另一種寫法,思路是:先創(chuàng)建子節(jié)點(diǎn)<CreatedBy><CreatedTime><SavedTime>,再創(chuàng)建父節(jié)點(diǎn)<CreationInfo>,然后把子節(jié)點(diǎn)添加到該父節(jié)點(diǎn)下面,再查找根節(jié)點(diǎn)<xxx>,最后把父節(jié)點(diǎn)<CreationInfo>添加到根節(jié)點(diǎn)末尾就OK了,代碼如下:

1 private static void CreateXwspFile(string fileName, string path) 2 { 3 XmlDocument xmlDoc = new XmlDocument(); 4 //創(chuàng)建類型聲明節(jié)點(diǎn) 5 XmlDeclaration xdDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); 6 xmlDoc.AppendChild(xdDec); 7 8 //創(chuàng)建根節(jié)點(diǎn) 9 XmlElement xeRoot = xmlDoc.CreateElement("xxx");10 //給節(jié)點(diǎn)屬性賦值11 xeRoot.SetAttribute("version", "1.0");12 xeRoot.SetAttribute("name", fileName);13 xmlDoc.AppendChild(xeRoot);14 15 ////創(chuàng)建并添加<CreationInfo></CreationInfo>節(jié)點(diǎn)16 ////創(chuàng)建并添加<CreatedBy></CreatedBy>節(jié)點(diǎn)17 ////創(chuàng)建并添加<CreatedTime></CreatedTime>節(jié)點(diǎn)18 ////創(chuàng)建并添加<SavedTime></SavedTime>節(jié)點(diǎn)19 XmlElement xeCreationInfo = xmlDoc.CreateElement("CreationInfo");20 XmlElement xeCreatedBy = xmlDoc.CreateElement("CreatedBy");21 xeCreatedBy.InnerText = "Tektronix Course Editor";22 XmlElement xeCreatedTime = xmlDoc.CreateElement("CreatedTime");23 xeCreatedTime.InnerText = DateTime.Now.ToString();24 XmlElement xeSavedTime = xmlDoc.CreateElement("SavedTime");25 xeSavedTime.InnerText = DateTime.Now.ToString();26 xeCreationInfo.AppendChild(xeCreatedBy);27 xeCreationInfo.AppendChild(xeCreatedTime);28 xeCreationInfo.AppendChild(xeSavedTime);29 XmlNode xnXwsp = xmlDoc.SelectSingleNode("xxx");30 if (xnXwsp != null)31 {32 xnXwsp.AppendChild(xeCreationInfo);33 }34 35 //創(chuàng)建并添加<a></a>節(jié)點(diǎn)36 xeRoot = xmlDoc.CreateElement("a");37 if (xnXwsp != null)38 {39 xnXwsp.AppendChild(xeRoot);40 }41 //創(chuàng)建并添加<b></b>節(jié)點(diǎn)42 xeRoot = xmlDoc.CreateElement("b");43 if (xnXwsp != null)44 {45 xnXwsp.AppendChild(xeRoot);46 }47 //創(chuàng)建并添加<c></c>節(jié)點(diǎn)48 xeRoot = xmlDoc.CreateElement("c");49 if (xnXwsp != null)50 {51 xnXwsp.AppendChild(xeRoot);52 }53 //保存XML文檔54 try55 {56 xmlDoc.Save(path + fileName + ".xwsp");57 }58 catch (Exception ep)59 {60 Console.WriteLine(ep.Message);61 }62 }
結(jié)果如下:

1 <?xml version="1.0" encoding="utf-8"?> 2 <xxx version="1.0" name="workspace1"> 3 <CreationInfo> 4 <CreatedBy>Tektronix Course Editor</CreatedBy> 5 <CreatedTime>2015/10/1 14:43:56</CreatedTime> 6 <SavedTime>2015/10/1 14:43:57</SavedTime> 7 </CreationInfo> 8 <a /> 9 <b />10 <c />11 </xxx>
現(xiàn)在還有一個(gè)問題沒有解決:
要在<?xml version="1.0" encoding="utf-8"?>節(jié)點(diǎn)的后面插入<!DOCTYPE xwsp>這個(gè)節(jié)點(diǎn),不曉得該怎么做,而且后面那個(gè)xwsp是可以改變的,意思就是可以自己定義,比如說我可以把它改為aaa之類的,這個(gè)暫時(shí)還沒找到解決方法
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注