/WIP/Users/TungTT/PC Commander v1.03/PC Commander/PC Commander/AppCode/FileReaderWriter.cs

http://cpro-wia.googlecode.com/ · C# · 330 lines · 204 code · 58 blank · 68 comment · 52 complexity · 04cb4298c8f7e4f04b742c603d274398 MD5 · raw file

  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using System.Xml;
  5. namespace PC_Commander.AppCode
  6. {
  7. class FileReaderWriter
  8. {
  9. #region Variables.
  10. // Variable
  11. private string strFilePath;
  12. private string stringData01;
  13. private string text;
  14. private int height;
  15. private int width;
  16. private int effectID;
  17. private int speed;
  18. #endregion
  19. #region Properities.
  20. public string StringData01
  21. {
  22. get { return stringData01; }
  23. set { stringData01 = value; }
  24. }
  25. public string Text
  26. {
  27. get { return text; }
  28. set { text = value; }
  29. }
  30. public int Height
  31. {
  32. get { return height; }
  33. set { height = value; }
  34. }
  35. public int Width
  36. {
  37. get { return width; }
  38. set { width = value; }
  39. }
  40. public int EffectID
  41. {
  42. get { return effectID; }
  43. set { effectID = value; }
  44. }
  45. public int Speed
  46. {
  47. get { return speed; }
  48. set { speed = value; }
  49. }
  50. #endregion
  51. #region Contructor.
  52. /// <summary>
  53. ///
  54. /// </summary>
  55. public FileReaderWriter(string path)
  56. {
  57. // Set xmlfile path
  58. strFilePath = path;
  59. }
  60. #endregion
  61. #region Other methods.
  62. /// <summary>
  63. ///
  64. /// </summary>
  65. /// <param name="ledTableName"></param>
  66. /// <param name="ledTableCardID"></param>
  67. public void GetDataFromXmlFile(string ledTableName, string ledTableCardID)
  68. {
  69. // Create new xml document
  70. XmlDocument xmlDocument = new XmlDocument();
  71. // Load File
  72. xmlDocument.Load(strFilePath);
  73. // Root element of Xml file.
  74. XmlElement rootElement = xmlDocument.DocumentElement;
  75. foreach (XmlNode node1 in rootElement.ChildNodes)
  76. if (node1.Attributes[0].Value == ledTableCardID && node1.Attributes[1].Value == ledTableName)
  77. {
  78. foreach (XmlNode node2 in node1.ChildNodes)
  79. if (node2.Name == "Height")
  80. height = Convert.ToInt32(node2.InnerText);
  81. else if (node2.Name == "Width")
  82. width = Convert.ToInt32(node2.InnerText);
  83. else if (node2.Name == "EffectID")
  84. effectID = Convert.ToInt32(node2.InnerText);
  85. else if (node2.Name == "Speed")
  86. speed = Convert.ToInt32(node2.InnerText);
  87. else if (node2.Name == "Data")
  88. stringData01 = node2.InnerText;
  89. else if (node2.Name == "Text")
  90. text = node2.InnerText;
  91. }
  92. }
  93. /// <summary>
  94. ///
  95. /// </summary>
  96. /// <param name="ledTableName"></param>
  97. /// <param name="ledTableCardID"></param>
  98. /// <param name="height"></param>
  99. /// <param name="width"></param>
  100. /// <param name="data"></param>
  101. public void CreateNewLedTable(string ledTableName, string ledTableCardID, int height, int width)
  102. {
  103. // Varialbes
  104. string data = "";
  105. for (int x = 0; x < height; x++)
  106. for (int y = 0; y < width; y++)
  107. {
  108. data += 0;
  109. }
  110. // Ma hoa strData01 sang ASCII
  111. ByteStringConverter convertBitsToString = new ByteStringConverter();
  112. Byte[] byteArray = convertBitsToString.ConvertBitsToBytes(data);
  113. data = convertBitsToString.ConvertByteToString(byteArray);
  114. // Create new xml document
  115. XmlDocument xmlDocument = new XmlDocument();
  116. // Load File
  117. xmlDocument.Load(strFilePath);
  118. // Root element of Xml file.
  119. XmlElement rootElement = xmlDocument.DocumentElement;
  120. // Create new element(new led table).
  121. XmlElement newElement = xmlDocument.CreateElement("Led");
  122. // Create attribute for this element.
  123. XmlAttribute attributeID = xmlDocument.CreateAttribute("ID");
  124. attributeID.Value = ledTableCardID;
  125. XmlAttribute attributeName = xmlDocument.CreateAttribute("Name");
  126. attributeName.Value = ledTableName ;
  127. // Append these attribute to new element.
  128. newElement.Attributes.Append(attributeID);
  129. newElement.Attributes.Append(attributeName);
  130. // Create new elements for this newElement.
  131. XmlElement heightElement = xmlDocument.CreateElement("Height");
  132. XmlElement widthElement = xmlDocument.CreateElement("Width");
  133. XmlElement effectIDtElement = xmlDocument.CreateElement("EffectID");
  134. XmlElement speedElement = xmlDocument.CreateElement("Speed");
  135. XmlElement dataElement = xmlDocument.CreateElement("Data");
  136. XmlElement textElement = xmlDocument.CreateElement("Text");
  137. heightElement.InnerText = height.ToString();
  138. widthElement.InnerText = width.ToString();
  139. effectIDtElement.InnerText = "0";
  140. speedElement.InnerText = "50";
  141. dataElement.InnerText = data;
  142. textElement.InnerText = "";
  143. // Append new elements to this newElement.
  144. newElement.AppendChild(heightElement);
  145. newElement.AppendChild(widthElement);
  146. newElement.AppendChild(effectIDtElement);
  147. newElement.AppendChild(speedElement);
  148. newElement.AppendChild(dataElement);
  149. newElement.AppendChild(textElement);
  150. // Append newElement to this rootElement.
  151. rootElement.AppendChild(newElement);
  152. // Save new change to xml file.
  153. xmlDocument.Save(strFilePath);
  154. }
  155. /// <summary>
  156. ///
  157. /// </summary>
  158. /// <param name="ledTableName"></param>
  159. /// <param name="ledTableCardID"></param>
  160. /// <param name="height"></param>
  161. /// <param name="width"></param>
  162. /// <param name="effectID"></param>
  163. /// <param name="speed"></param>
  164. /// <param name="data"></param>
  165. public bool SaveLedTableToXML(string ledTableName, string ledTableCardID, int height, int width,int effectID,int speed,string stringData01,string text)
  166. {
  167. // Create new xml document
  168. XmlDocument xmlDocument = new XmlDocument();
  169. try
  170. {
  171. // Load File
  172. xmlDocument.Load(strFilePath);
  173. // Root element of Xml file.
  174. XmlElement rootElement = xmlDocument.DocumentElement;
  175. foreach (XmlNode node1 in rootElement.ChildNodes)
  176. //foreach (XmlNode node2 in node1.ChildNodes)
  177. if (node1.Attributes[0].Value == ledTableCardID && node1.Attributes[1].Value == ledTableName)
  178. {
  179. foreach (XmlNode node2 in node1.ChildNodes)
  180. if (node2.Name == "Height")
  181. node2.InnerText = height.ToString();
  182. else if (node2.Name == "Width")
  183. node2.InnerText = width.ToString();
  184. else if (node2.Name == "EffectID")
  185. node2.InnerText = effectID.ToString();
  186. else if (node2.Name == "Speed")
  187. node2.InnerText = speed.ToString();
  188. else if (node2.Name == "Data")
  189. node2.InnerText = stringData01;
  190. else if (node2.Name == "Text")
  191. node2.InnerText = text;
  192. }
  193. // Save new change to xml file.
  194. xmlDocument.Save(strFilePath);
  195. // Return
  196. return true;
  197. }
  198. catch (Exception)
  199. {
  200. // Return.
  201. return false;
  202. }
  203. }
  204. public bool SaveLedTableName(string newLedTableName, string ledTableCardID)
  205. {
  206. // Create new xml document
  207. XmlDocument xmlDocument = new XmlDocument();
  208. try
  209. {
  210. // Load File
  211. xmlDocument.Load(strFilePath);
  212. // Root element of Xml file.
  213. XmlElement rootElement = xmlDocument.DocumentElement;
  214. foreach (XmlNode node1 in rootElement.ChildNodes)
  215. if (node1.Attributes[0].Value == ledTableCardID )
  216. {
  217. node1.Attributes[1].Value = newLedTableName;
  218. }
  219. // Save new change to xml file.
  220. xmlDocument.Save(strFilePath);
  221. // Return
  222. return true;
  223. }
  224. catch (Exception)
  225. {
  226. // Return.
  227. return false;
  228. }
  229. }
  230. /// <summary>
  231. ///
  232. /// </summary>
  233. /// <param name="ledTableName"></param>
  234. /// <param name="ledTableCardID"></param>
  235. public void DeleteLedTableInXML(string ledTableName, string ledTableCardID)
  236. {
  237. // Create new xml document
  238. XmlDocument xmlDocument = new XmlDocument();
  239. // Load File
  240. xmlDocument.Load(strFilePath);
  241. // Root element of Xml file.
  242. XmlElement rootElement = xmlDocument.DocumentElement;
  243. foreach (XmlNode node1 in rootElement.ChildNodes)
  244. if (node1.Attributes[0].Value == ledTableCardID && node1.Attributes[1].Value == ledTableName)
  245. {
  246. rootElement.RemoveChild(node1);
  247. }
  248. // Save new change to xml file.
  249. xmlDocument.Save(strFilePath);
  250. //return true;
  251. }
  252. public bool CheckLedTableCardID(string ledTableCardID)
  253. {
  254. bool isOK = false;
  255. XmlDocument xmlDocument = new XmlDocument();
  256. // Load File
  257. xmlDocument.Load(strFilePath);
  258. // Root element of Xml file.
  259. XmlElement rootElement = xmlDocument.DocumentElement;
  260. foreach (XmlNode node1 in rootElement.ChildNodes)
  261. if (node1.Attributes[0].Value == ledTableCardID )
  262. {
  263. isOK = true;
  264. break;
  265. }
  266. else
  267. isOK = false;
  268. return isOK;
  269. }
  270. #endregion
  271. }
  272. }