Example #1
0
        // Executing operations against each file in a fileArray
        private static Tuple <string, int> ExecuteFileOperations(string filePath)
        {
            FolderMain xData = ValidateDeserializeData(filePath);

            if (xData != null)
            {
                List <Folder> folderList     = xData.Folder;
                int           calculationNum = 0;
                double        result         = 0.0;

                foreach (var value in folderList)
                {
                    try
                    {
                        Operands op  = value.StrSecond.Operand;
                        int      mod = Int32.Parse(value.Mod.Value);
                        result = CalculateFileResult(result, mod, op);
                        calculationNum++;
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.Message);
                        continue;
                    }
                }

                Console.WriteLine("File: {0}", filePath);
                Console.WriteLine("Result is: {0}", result);
                Console.WriteLine("Number of calculations: {0}", calculationNum);
                Console.WriteLine();

                var fileOutTuple = new Tuple <string, int>(filePath, calculationNum);

                return(fileOutTuple);
            }
            else
            {
                return(null);
            }
        }
Example #2
0
        // Validation and deseralization
        private static FolderMain ValidateDeserializeData(string filePath)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(FolderMain));
            FolderMain    xmlData      = null;

            XmlReaderSettings settings = new XmlReaderSettings();

            settings.ValidationType = ValidationType.Schema;
            settings.Schemas.Add(null, "XmlSchema.xsd");
            settings.ValidationFlags        |= XmlSchemaValidationFlags.ProcessInlineSchema;
            settings.ValidationFlags        |= XmlSchemaValidationFlags.ReportValidationWarnings;
            settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);

            XmlReader reader = XmlReader.Create(filePath, settings);

            try
            {
                xmlData = (FolderMain)deserializer.Deserialize(reader);
            }
            catch (InvalidOperationException e)
            {
                Debug.WriteLine("Debug: " + e.Message);
                Console.WriteLine("Wrong operation type is provided for file {0}. \n" +
                                  "Check operand attributes. Only \"add\", \"divide\", \"multiply\" and \"subtract\" operations are possible. \n", filePath);
            }

            void ValidationCallback(object sender, ValidationEventArgs e)
            {
                Console.Write(e.Severity + ": ");
                Console.WriteLine(e.Message);
                Console.WriteLine("File: {0}, Number of line: {1}, Number of position: {2}", filePath, e.Exception.LineNumber, e.Exception.LinePosition);
                Console.WriteLine();
            }

            return(xmlData);
        }