之前的項目需要涉及到對XML文件的讀寫,由於之前沒怎麼接觸過這方面的知識,於是在網上查找並試驗出了具體的實現方法:
第一種方法:
1 //動態的一個節點一個節點的生成XML文件 2 public void CreateXmlFile(string aaa) 3 { 4 XmlDocument xmlDoc = new XmlDocument(); 5 xmlDoc.CreateDocumentType("plist", "", "", ""); 6 //創建類型聲明節點 7 XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", ""); 8 xmlDoc.AppendChild(node); 9 XmlDocumentType docType = new XmlDocumentType(); 10 // node.InnerXml="<!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTDs/PropertyList-1.0.dtd' >"; 11 //創建根節點 12 XmlNode root = xmlDoc.CreateElement("plist"); 13 xmlDoc.AppendChild(root); 14 15 XmlNode dict = xmlDoc.CreateNode(XmlNodeType.Element, "dict",null); 16 CreateNode(xmlDoc, dict, "key", "items"); 17 18 root.AppendChild(dict); 19 XmlNode array = xmlDoc.CreateElement("array"); 20 21 XmlNode dict1 = xmlDoc.CreateElement("dict"); 22 CreateNode(xmlDoc, dict1, "key", "assets"); 23 24 XmlNode array1 = xmlDoc.CreateElement("array"); 25 26 XmlNode dict2 = xmlDoc.CreateElement("dict"); 27 CreateNode(xmlDoc, dict2, "key", "kind"); 28 CreateNode(xmlDoc, dict2, "string", "software-package"); 29 CreateNode(xmlDoc, dict2, "key", "url"); 30 //文件下載地址 31 CreateNode(xmlDoc, dict2, "string", "https://app.yuntian.net/ipa/DealerPortal_1.0.ipa"); 32 array1.AppendChild(dict2); 33 XmlNode dict3 = xmlDoc.CreateElement("dict"); 34 CreateNode(xmlDoc, dict3, "key", "kind"); 35 CreateNode(xmlDoc, dict3, "string", "full-size-image"); 36 CreateNode(xmlDoc, dict3, "key", "needs-shine"); 37 XmlNode true1 = xmlDoc.CreateElement("true"); 38 dict3.AppendChild(true1); 39 CreateNode(xmlDoc, dict3, "key", "url"); 40 //http或者https鏈接的圖片地址,可直接使用app對應的icon(分辨率沒強制要求) 41 CreateNode(xmlDoc, dict3, "string", "https://app.yuntian.net/images/ios1.png"); 42 array1.AppendChild(dict3); 43 XmlNode dict4 = xmlDoc.CreateElement("dict"); 44 CreateNode(xmlDoc, dict4, "key", "kind"); 45 CreateNode(xmlDoc, dict4, "string", "display-image"); 46 CreateNode(xmlDoc, dict4, "key", "needs-shine"); 47 XmlNode true2 = xmlDoc.CreateElement("true"); 48 dict4.AppendChild(true2); 49 CreateNode(xmlDoc, dict4, "key", "url"); 50 //http或者https鏈接的圖片地址,可直接使用app對應的icon(分辨率沒強制要求) 51 CreateNode(xmlDoc, dict4, "string", "https://app.yuntian.net/images/xlogo.png"); 52 array1.AppendChild(dict4); 53 54 55 dict1.AppendChild(array1); 56 57 CreateNode(xmlDoc, dict1, "key", "metadata"); 58 XmlNode dict5 = xmlDoc.CreateElement("dict"); 59 CreateNode(xmlDoc, dict5, "key", "bundle-identifier"); 60 CreateNode(xmlDoc, dict5, "string", aaa); 61 CreateNode(xmlDoc, dict5, "string", "0.0.1"); 62 CreateNode(xmlDoc, dict5, "key", "kind"); 63 CreateNode(xmlDoc, dict5, "string", "software"); 64 CreateNode(xmlDoc, dict5, "key", "title"); 65 CreateNode(xmlDoc, dict5, "string", "經銷商門戶"); 66 67 dict1.AppendChild(dict5); 68 array.AppendChild(dict1); 69 dict.AppendChild(array); 70 71 72 try 73 { 74 xmlDoc.Save("F:/XML/data5.plist"); 75 } 76 catch (Exception e) 77 { 78 //顯示錯誤信息 79 Console.WriteLine(e.Message); 80 } 81 //Console.ReadLine(); 82 83 } 84 public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value) 85 { 86 XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null); 87 node.InnerText = value; 88 parentNode.AppendChild(node); 89 } View Code這種方法比較笨,一個個節點的創建,不靈活,並且容易出錯
第二種方法:
//直接根據XML文件的內容生成XML文件 // public void createXmlLode() // { // XmlDocument xmldoc = new XmlDocument(); //創建空的XML文檔 // xmldoc.LoadXml("<?xml version='1.0' encoding='UTF-8'?>" + //"<!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTDs/PropertyList-1.0.dtd' >" + //"<plist version='1.0'>" + // "<dict>" + // " <key>items</key>" + // "<array>" + // " <dict>" + // " <key>assets</key>" + // "<array>" + // " <dict>" + // " <key>kind</key>" + // " <string>software-package</string>" + // " <key>url</key>" + // " <string>https://app.yuntian.net/ipa/DealerPortal_1.0.ipa</string>" + // " </dict>" + // " <dict>" + // " <key>kind</key>" + // " <string>full-size-image</string>" + // " <key>needs-shine</key>" + // " <true/>" + // " <key>url</key>" + // " <string>https://app.yuntian.net/images/ios1.png</string>" + // "</dict>" + // "<dict>" + // " <key>kind</key>" + // " <string>display-image</string>" + // " <key>needs-shine</key>" + // " <true/>" + // " <key>url</key>" + // " <string>https://app.yuntian.net/images/xlogo.png</string>" + // "</dict>" + // "</array>" + // " <key>metadata</key>" + // " <dict>" + // " <key>bundle-identifier</key>" + // " <string>com.ytsoft.dealerportal</string>" + // " <key>bundle-version</key>" + // " <string>0.0.1</string>" + // " <key>kind</key>" + // " <string>software</string>" + // " <key>title</key>" + // " <string>經銷商門戶</string>" + // " </dict>" + // "</dict>" + // "</array>" + // " </dict>" + //"</plist>"); // xmldoc.Save("F:/XML/ppp.plist"); //保存 // } View Code一股腦的將所有的XML文件手寫出來,然後生成。說實話,也挺麻煩的。就找到了這兩種方法,最後發現都挺麻煩。
最後在組長的指導下,根據現有的模板自動生成Xml文件,雖然代碼寫得有點戳,但是基本自己完成,好歹算個進步吧。
/// <summary> /// 根據模板動態生成XML文件 /// </summary> /// <param name="DownUrl">安裝包下載地址</param> /// <param name="ImageUrl">圖片地址</param> /// <param name="SmallImageUrl">小圖標地址</param> /// <param name="AppName">app包名</param> /// <param name="version">版本號</param> /// <param name="title">標題</param> public void CreateXMLByTemplet(string DownUrl, string ImageUrl, string SmallImageUrl, String AppName, string version,string title) { try { XmlDocument doc = new XmlDocument(); //加載Xml文件 string Templeturl="http://localhost:8031/Approval.7.3.plist"; if (File.Exists(Templeturl)) { Alert.Show("無此文件!"); } doc.Load(Templeturl); XmlElement rootElem = doc.DocumentElement; //獲取根節點 XmlNodeList dNode = rootElem.GetElementsByTagName("dict"); //獲取dict子節點集合 foreach (XmlNode node in dNode) { XmlElement xe = (XmlElement)node; // 得到Type和ISBN兩個屬性的屬性值 XmlNodeList xnl0 = xe.ChildNodes; for (int i = 0; i < xnl0.Count; i++) { if (xnl0.Item(i).InnerText == "software-package") { xnl0.Item(i + 3).InnerText = DownUrl; } if (xnl0.Item(i).InnerText == "full-size-image") { xnl0.Item(i + 5).InnerText = ImageUrl; } if (xnl0.Item(i).InnerText == "display-image") { xnl0.Item(i + 5).InnerText = SmallImageUrl; } if (xnl0.Item(i).InnerText == "bundle-identifier") { xnl0.Item(i + 2).InnerText = AppName; } if (xnl0.Item(i).InnerText == "bundle-version") { xnl0.Item(i + 2).InnerText = version; } if (xnl0.Item(i).InnerText == "title") { xnl0.Item(i + 2).InnerText = title; } } } doc.Save("F:/XML/aaa.plist"); Alert.Show("保存成功!"); } catch (Exception ex) { Alert.Show("保存失敗!"+ex); } }
另外,需要注意的是,在XML文件中,注釋也算節點的。。