Generate an XML file ., Generate xml file
The previous project involves reading and writing XML files. Since I have never been familiar with this knowledge before, I found and tested the specific implementation methods on the Internet:
Method 1:
1 // dynamically generate XML file 2 public void CreateXmlFile (string aaa) 3 {4 XmlDocument xmlDoc = new XmlDocument (); 5 xmlDoc. createDocumentType ("plist", ""); 6 // create a type declaration node 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 // create the root node 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 // file 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 // The image address of the http or https link, you can directly use the icon corresponding to the app (resolution not required) 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 or https link image address, you can directly use the icon corresponding to the app (resolution not required) 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 ", "dealer Portal"); 66 67 dict1.AppendChild (dict5); 68 array. appendChild (dict1); 69 dict. appendChild (array); 70 71 72 try73 {74 xmlDoc. save ("F:/XML/data5.plist"); 75} 76 catch (Exception e) 77 {78 // Display error message 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
This method is stupid. It creates nodes one by one. It is not flexible and error-prone.
Method 2:
// Generate the XML file directly based on the content of the XML file // public void createXmlLode () // {// XmlDocument xmldoc = new XmlDocument (); // create an empty XML document // 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> dealer portal </string> "+ //" </dict> "+ //" </dict> "+ //" </array> "+ //" </array>/ dict> "+ //" </plist> "); // xmldoc. save ("F:/XML/ppp. plist "); // save //}View Code
All XML files are hand-written by a brain and then generated. To be honest, it's quite troublesome. These two methods are found to be quite troublesome.
Finally, under the guidance of the team leader, the Xml file is automatically generated based on the existing template. Although the Code is a little signed, it is basically done by yourself.
/// <Summary> /// dynamically generate an XML file based on the template /// </summary> /// <param name = "DownUrl"> Installation Package </param> // /<param name = "ImageUrl"> image address </param> // <param name = "SmallImageUrl"> small icon address </param> // <param name =" appName "> app package name </param> /// <param name =" version "> version </param> /// <param name =" title "> title </ param> public void CreateXMLByTemplet (string DownUrl, string ImageUrl, string SmallImageUrl, String AppName, string version, string title) {try {XmlDocument doc = new XmlDocument (); // load the Xml file string Templeturl =" http://localhost:8031/Approval.7.3.plist "; If (File. Exists (Templeturl) {Alert. Show (" No such File! ");} Doc. load (Templeturl); XmlElement rootElem = doc. documentElement; // get the root node XmlNodeList dNode = rootElem. getElementsByTagName ("dict"); // obtain the dict subnode set foreach (XmlNode node in dNode) {XmlElement xe = (XmlElement) node; // obtain the attribute value XmlNodeList xnl0 = xe For the Type and ISBN attributes. childNodes; for (int I = 0; I <xnl0.Count; I ++) {if (xnl0.Item (I ). innerText = "software-package") {xnl0.Item (I + 3 ). innerText = Down Url;} 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 ). innerT Ext = "title") {xnl0.Item (I + 2 ). innerText = title ;}} doc. save ("F:/XML/aaa. plist "); Alert. show ("saved successfully! ");} Catch (Exception ex) {Alert. Show (" failed to save! "+ Ex );}}
In addition, note that annotations in XML files are also node-specific ..