Esempio n. 1
0
        /// <summary>
        /// Tries to load all routing tables from a directory.
        /// You could have several XML serialized routing tables. This way allows to share it via plugins.
        /// </summary>
        /// <param name="directory">A root folder.</param>
        /// <param name="searchOption">Definds does tne search will applied to child folders.</param>
        /// <returns>A found table. Null if not found.</returns>
        public static RoutingTable LoadRoutingTables(string directory, SearchOption searchOption)
        {
            // Create new empty table.
            RoutingTable table = new RoutingTable();

            // Check directory exist.
            if (!Directory.Exists(directory))
            {
                // Create if not found.
                Directory.CreateDirectory(directory);
            }

            // Detect all xml files in directory.
            string[] xmlFiles = Directory.GetFiles(directory, "*.xml", searchOption);

            // Init encoder.
            XmlSerializer xmlSer = new XmlSerializer(typeof(RoutingTable), Instruction.DerivedTypes);

            // Deserialize every file to table if possible.
            foreach (string fileDir in xmlFiles)
            {
                // Open stream to XML file.
                using (FileStream fs = new FileStream(fileDir, FileMode.Open))
                {
                    RoutingTable tableBufer = null;
                    try
                    {
                        // Try to deserialize routing table from file.
                        tableBufer = xmlSer.Deserialize(fs) as RoutingTable;

                        // Buferize directory to backward access.
                        tableBufer.SourcePath = fileDir;

                        // Add to  common table.
                        table += tableBufer;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("ROUTING TABLE ERROR: File reading failed. Reason:\n{0}\n", ex.Message);
                    }
                }
            }

            return(table);
        }
Esempio n. 2
0
        /// <summary>
        /// Saves a routing table by directory.
        /// </summary>
        /// <param name="table">A source with instructions.</param>
        /// <param name="directory">A target directory.</param>
        /// <param name="name">A name of the file.</param>
        public static void SaveRoutingTable(RoutingTable table, string directory, string name)
        {
            #region Validate directory
            // Avoid null reference exception.
            if (directory == null)
            {
                directory = "";
            }

            // Check directory exist.
            if (directory != "" &&
                !Directory.Exists(directory))
            {
                // Create if not found.
                Directory.CreateDirectory(directory);
            }
            #endregion

            #region Write table to XML file.
            try
            {
                XmlDocument   xmlDocument = new XmlDocument();
                XmlSerializer serializer  = new XmlSerializer(typeof(RoutingTable), Instruction.DerivedTypes);
                using (MemoryStream stream = new MemoryStream())
                {
                    serializer.Serialize(stream, table);
                    stream.Position = 0;
                    xmlDocument.Load(stream);
                    xmlDocument.Save(directory + "/" + name + ".xml");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ROUTING TABLE ERROR: Not serialized. Reason:\n{0}", ex.Message);
            }
            #endregion
        }
Esempio n. 3
0
 /// <summary>
 /// Saves a routing table by the default directory
 /// %app/resources/routing
 /// </summary>
 /// <param name="table">A source with instructions.</param>
 /// <param name="name">A name of the file.</param>
 public static void SaveRoutingTable(RoutingTable table, string name)
 {
     SaveRoutingTable(table, "resources/routing", name);
 }
Esempio n. 4
0
 /// <summary>
 /// Saves a routing table by the default directory
 /// %app/resources/routing/ROUTING.xml
 /// </summary>
 /// <param name="table">A source with instructions.</param>
 public static void SaveRoutingTable(RoutingTable table)
 {
     SaveRoutingTable(table, "resources/routing", "ROUTING");
 }