国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

C#操作XML學(xué)習(xí)之創(chuàng)建XML文件的同時(shí)新建根節(jié)點(diǎn)和子節(jié)點(diǎn)(多級(jí)子節(jié)點(diǎn))

2019-11-14 13:56:17
字體:
供稿:網(wǎng)友

最近工作中遇到一個(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>
View Code

首先第一個(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         }
View Code

結(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>
View Code

子節(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         }
View Code

結(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>
View Code

 

現(xiàn)在還有一個(gè)問題沒有解決:

要在<?xml version="1.0" encoding="utf-8"?>節(jié)點(diǎn)的后面插入<!DOCTYPE xwsp>這個(gè)節(jié)點(diǎn),不曉得該怎么做,而且后面那個(gè)xwsp是可以改變的,意思就是可以自己定義,比如說我可以把它改為aaa之類的,這個(gè)暫時(shí)還沒找到解決方法


上一篇:C#預(yù)定義類型

下一篇:高斯算法

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 弋阳县| 炉霍县| 来宾市| 察雅县| 阳城县| 商河县| 涟水县| 济宁市| 定南县| 公安县| 高清| 灌南县| 广南县| 扎兰屯市| 始兴县| 峨边| 卢龙县| 乌兰察布市| 余干县| 滨海县| 双鸭山市| 浮梁县| 香河县| 贵港市| 邓州市| 宝坻区| 铜梁县| 昌图县| 昆明市| 龙门县| 赞皇县| 巴彦县| 扎赉特旗| 梅州市| 三亚市| 西盟| 合阳县| 独山县| 集贤县| 营山县| 厦门市|