Example #1
0
        // performs computation results serialization to specified *.xml file
        public static bool SaveToXML(string fileName, OutputData data)
        {
            bool serializationResult = true;

            var writer = new XmlSerializer(typeof(OutputData));

            // deleting all schema info from root tag
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            ns.Add("", "");

            FileStream fs = null;

            try
            {
                fs = new FileStream(fileName, FileMode.Create);
            }
            catch (IOException exc)
            {
                Interface.DisplayMessage("Error in OutputData->SaveToXML: Unable to open file \"" +
                                         fileName + "\"", ConsoleColor.Yellow, ConsoleColor.Red);
                Interface.DisplayMessage("Description: " + exc, ConsoleColor.Yellow, ConsoleColor.Blue);

                return(false);
            }

            var stream = new StreamWriter(fs, Encoding.UTF8);

            try
            {
                writer.Serialize(stream, data, ns);
                stream.Flush();
            }
            catch (IOException exc)
            {
                Interface.DisplayMessage("Error in OutputData->SaveToXML: I/O error while " +
                                         "serializing data to file \"" + fileName + "\"",
                                         ConsoleColor.Yellow, ConsoleColor.Red);
                Interface.DisplayMessage("Description: " + exc, ConsoleColor.Yellow,
                                         ConsoleColor.Blue);

                serializationResult = false;
            }
            finally
            {
                stream.Close();
            }

            return(serializationResult);
        }
Example #2
0
        public static void Main(string[] args)
        {
            Console.Title = "Calculator via XML";

            // checking command line arguments number
            if (args.Length != 2)
            {
                Console.WriteLine("Illegal arguments number\nUsage: Calculator.exe " +
                                  "<InputFileName.xml> <OutputFileName.xml>");
                return;
            }

            // creating calculator instance
            Computation calc = new Computation();

            // reading operand and operation sets from input *.xml
            InputData input = InputData.ParseXML(args[0]);

            if (input == null)
            {
                return;
            }

            // creating storage for operation results saving
            OutputData output = new OutputData();

            OperandElement result;

            // performing computation
            for (int i = 0; i < input.InputElements.Count; i += 3)
            {
                result = calc.Compute((OperandElement)input.InputElements[i],
                                      (OperationElement)input.InputElements[i + 1],
                                      (OperandElement)input.InputElements[i + 2]);

                output.OutputElements.Add(result);
            }

            // saving computation results to *.xml
            OutputData.SaveToXML(args[1], output);
        }