Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. public bool SaveXML(string filename)
  2.         {
  3.             try
  4.             {
  5.                 XmlDocument document = new XmlDocument();
  6.  
  7.                 XmlDeclaration declaration = document.CreateXmlDeclaration("1.0", null, null);
  8.                 declaration.Encoding = "utf-8";
  9.                 document.AppendChild(declaration);
  10.  
  11.                 XmlElement root = document.CreateElement("", "Bank", "");
  12.                 root.SetAttribute("version", "1"); // Don't know what this is for
  13.                 document.AppendChild(root);
  14.  
  15.                 // Add different race sections
  16.                 XmlElement protossElement = document.CreateElement("", "Section", "");
  17.                 protossElement.SetAttribute("name", "ProtossBuildOrders");
  18.                 XmlElement zergElement = document.CreateElement("", "Section", "");
  19.                 zergElement.SetAttribute("name", "ZergBuildOrders");
  20.                 XmlElement terranElement = document.CreateElement("", "Section", "");
  21.                 terranElement.SetAttribute("name", "TerranBuildOrders");
  22.  
  23.                 root.AppendChild(protossElement);
  24.                 root.AppendChild(zergElement);
  25.                 root.AppendChild(terranElement);
  26.  
  27.                 // Add the builds to the XML file
  28.                 foreach (BuildOrder build in buildOrders)
  29.                 {
  30.                     XmlElement key = document.CreateElement("Key");
  31.                     key.SetAttribute("name", build.Key);
  32.  
  33.                     XmlElement value = document.CreateElement("Value");
  34.                     value.SetAttribute("string", build.Value);
  35.  
  36.                     key.AppendChild(value);
  37.  
  38.                     switch (build.Race)
  39.                     {
  40.                         case (BuildRace.Protoss):
  41.                             protossElement.AppendChild(key);
  42.                             break;
  43.                         case (BuildRace.Zerg):
  44.                             zergElement.AppendChild(key);
  45.                             break;
  46.                         case (BuildRace.Terran):
  47.                             terranElement.AppendChild(key);
  48.                             break;
  49.                     }
  50.                 }
  51.  
  52.                 document.Save(filename);
  53.                 return true;
  54.             }
  55.             catch
  56.             {
  57.                 return false;
  58.             }            
  59.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement