/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
- using System;
- using System.IO;
- using System.Windows.Forms;
- using System.Xml;
-
- namespace PC_Commander.AppCode
- {
- class FileReaderWriter
- {
-
- #region Variables.
-
- // Variable
- private string strFilePath;
- private string stringData01;
- private string text;
- private int height;
- private int width;
- private int effectID;
- private int speed;
-
-
- #endregion
-
- #region Properities.
-
- public string StringData01
- {
- get { return stringData01; }
- set { stringData01 = value; }
- }
-
- public string Text
- {
- get { return text; }
- set { text = value; }
- }
- public int Height
- {
- get { return height; }
- set { height = value; }
- }
- public int Width
- {
- get { return width; }
- set { width = value; }
- }
- public int EffectID
- {
- get { return effectID; }
- set { effectID = value; }
- }
- public int Speed
- {
- get { return speed; }
- set { speed = value; }
- }
- #endregion
-
- #region Contructor.
- /// <summary>
- ///
- /// </summary>
- public FileReaderWriter(string path)
- {
- // Set xmlfile path
- strFilePath = path;
- }
- #endregion
-
- #region Other methods.
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="ledTableName"></param>
- /// <param name="ledTableCardID"></param>
- public void GetDataFromXmlFile(string ledTableName, string ledTableCardID)
- {
-
- // Create new xml document
- XmlDocument xmlDocument = new XmlDocument();
-
- // Load File
- xmlDocument.Load(strFilePath);
-
- // Root element of Xml file.
- XmlElement rootElement = xmlDocument.DocumentElement;
-
- foreach (XmlNode node1 in rootElement.ChildNodes)
- if (node1.Attributes[0].Value == ledTableCardID && node1.Attributes[1].Value == ledTableName)
- {
- foreach (XmlNode node2 in node1.ChildNodes)
- if (node2.Name == "Height")
- height = Convert.ToInt32(node2.InnerText);
- else if (node2.Name == "Width")
- width = Convert.ToInt32(node2.InnerText);
- else if (node2.Name == "EffectID")
- effectID = Convert.ToInt32(node2.InnerText);
- else if (node2.Name == "Speed")
- speed = Convert.ToInt32(node2.InnerText);
- else if (node2.Name == "Data")
- stringData01 = node2.InnerText;
- else if (node2.Name == "Text")
- text = node2.InnerText;
- }
- }
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="ledTableName"></param>
- /// <param name="ledTableCardID"></param>
- /// <param name="height"></param>
- /// <param name="width"></param>
- /// <param name="data"></param>
- public void CreateNewLedTable(string ledTableName, string ledTableCardID, int height, int width)
- {
- // Varialbes
- string data = "";
- for (int x = 0; x < height; x++)
- for (int y = 0; y < width; y++)
- {
- data += 0;
- }
-
- // Ma hoa strData01 sang ASCII
- ByteStringConverter convertBitsToString = new ByteStringConverter();
- Byte[] byteArray = convertBitsToString.ConvertBitsToBytes(data);
- data = convertBitsToString.ConvertByteToString(byteArray);
-
-
- // Create new xml document
- XmlDocument xmlDocument = new XmlDocument();
-
- // Load File
- xmlDocument.Load(strFilePath);
-
- // Root element of Xml file.
- XmlElement rootElement = xmlDocument.DocumentElement;
-
- // Create new element(new led table).
- XmlElement newElement = xmlDocument.CreateElement("Led");
-
- // Create attribute for this element.
- XmlAttribute attributeID = xmlDocument.CreateAttribute("ID");
- attributeID.Value = ledTableCardID;
- XmlAttribute attributeName = xmlDocument.CreateAttribute("Name");
- attributeName.Value = ledTableName ;
-
- // Append these attribute to new element.
- newElement.Attributes.Append(attributeID);
- newElement.Attributes.Append(attributeName);
-
- // Create new elements for this newElement.
- XmlElement heightElement = xmlDocument.CreateElement("Height");
- XmlElement widthElement = xmlDocument.CreateElement("Width");
- XmlElement effectIDtElement = xmlDocument.CreateElement("EffectID");
- XmlElement speedElement = xmlDocument.CreateElement("Speed");
- XmlElement dataElement = xmlDocument.CreateElement("Data");
- XmlElement textElement = xmlDocument.CreateElement("Text");
-
- heightElement.InnerText = height.ToString();
- widthElement.InnerText = width.ToString();
- effectIDtElement.InnerText = "0";
- speedElement.InnerText = "50";
- dataElement.InnerText = data;
- textElement.InnerText = "";
-
- // Append new elements to this newElement.
- newElement.AppendChild(heightElement);
- newElement.AppendChild(widthElement);
- newElement.AppendChild(effectIDtElement);
- newElement.AppendChild(speedElement);
- newElement.AppendChild(dataElement);
- newElement.AppendChild(textElement);
-
- // Append newElement to this rootElement.
- rootElement.AppendChild(newElement);
-
- // Save new change to xml file.
- xmlDocument.Save(strFilePath);
-
- }
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="ledTableName"></param>
- /// <param name="ledTableCardID"></param>
- /// <param name="height"></param>
- /// <param name="width"></param>
- /// <param name="effectID"></param>
- /// <param name="speed"></param>
- /// <param name="data"></param>
- public bool SaveLedTableToXML(string ledTableName, string ledTableCardID, int height, int width,int effectID,int speed,string stringData01,string text)
- {
- // Create new xml document
- XmlDocument xmlDocument = new XmlDocument();
-
- try
- {
- // Load File
- xmlDocument.Load(strFilePath);
-
- // Root element of Xml file.
- XmlElement rootElement = xmlDocument.DocumentElement;
-
- foreach (XmlNode node1 in rootElement.ChildNodes)
- //foreach (XmlNode node2 in node1.ChildNodes)
- if (node1.Attributes[0].Value == ledTableCardID && node1.Attributes[1].Value == ledTableName)
- {
- foreach (XmlNode node2 in node1.ChildNodes)
- if (node2.Name == "Height")
- node2.InnerText = height.ToString();
- else if (node2.Name == "Width")
- node2.InnerText = width.ToString();
- else if (node2.Name == "EffectID")
- node2.InnerText = effectID.ToString();
- else if (node2.Name == "Speed")
- node2.InnerText = speed.ToString();
- else if (node2.Name == "Data")
- node2.InnerText = stringData01;
- else if (node2.Name == "Text")
- node2.InnerText = text;
- }
-
- // Save new change to xml file.
- xmlDocument.Save(strFilePath);
-
- // Return
- return true;
- }
- catch (Exception)
- {
- // Return.
- return false;
- }
- }
-
- public bool SaveLedTableName(string newLedTableName, string ledTableCardID)
- {
- // Create new xml document
- XmlDocument xmlDocument = new XmlDocument();
-
- try
- {
- // Load File
- xmlDocument.Load(strFilePath);
-
- // Root element of Xml file.
- XmlElement rootElement = xmlDocument.DocumentElement;
-
- foreach (XmlNode node1 in rootElement.ChildNodes)
- if (node1.Attributes[0].Value == ledTableCardID )
- {
- node1.Attributes[1].Value = newLedTableName;
- }
-
- // Save new change to xml file.
- xmlDocument.Save(strFilePath);
-
- // Return
- return true;
- }
- catch (Exception)
- {
- // Return.
- return false;
- }
- }
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="ledTableName"></param>
- /// <param name="ledTableCardID"></param>
- public void DeleteLedTableInXML(string ledTableName, string ledTableCardID)
- {
- // Create new xml document
- XmlDocument xmlDocument = new XmlDocument();
-
- // Load File
- xmlDocument.Load(strFilePath);
-
- // Root element of Xml file.
- XmlElement rootElement = xmlDocument.DocumentElement;
-
- foreach (XmlNode node1 in rootElement.ChildNodes)
- if (node1.Attributes[0].Value == ledTableCardID && node1.Attributes[1].Value == ledTableName)
- {
- rootElement.RemoveChild(node1);
- }
-
- // Save new change to xml file.
- xmlDocument.Save(strFilePath);
-
- //return true;
- }
-
-
- public bool CheckLedTableCardID(string ledTableCardID)
- {
- bool isOK = false;
-
- XmlDocument xmlDocument = new XmlDocument();
-
- // Load File
- xmlDocument.Load(strFilePath);
-
- // Root element of Xml file.
- XmlElement rootElement = xmlDocument.DocumentElement;
-
- foreach (XmlNode node1 in rootElement.ChildNodes)
- if (node1.Attributes[0].Value == ledTableCardID )
- {
-
- isOK = true;
- break;
- }
- else
- isOK = false;
-
-
- return isOK;
-
- }
- #endregion
- }
- }