Example #1
0
        /// <summary>
        /// Tries to open compiled XML file at the given path in the editor.
        /// </summary>
        /// <param name="filePath"></param>
        private void OpenFile(string filePath)
        {
            // Check file's existence
            if (!File.Exists(filePath))
            {
                MessageBox.Show("File not found: " + filePath, _windowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Check file extension
            if (!Path.GetFileName(filePath).EndsWith(".xml.compiled"))
            {
                MessageBox.Show("Unknown file type, expected: *.xml.compiled", _windowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Read file
            try
            {
                string xml;
                using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    xml = FeaturesFile.CompiledToXml(fs);

                this.TxtEditor.Text = xml;
                this.Text           = _windowTitle + " - " + filePath;

                _openedFilePath = filePath;

                var known = Regex.Matches(xml, @"Name=""[^\?\""]+""").Count;
                var total = Regex.Matches(xml, @"Name=""").Count;
                this.LblKnownFeatureCount.Text = string.Format("Known features: {0}/{1}", known, total);

                _fileChanged = false;
                this.ResetUndo();
                this.UpdateSaveButtons();
                this.UpdateFeatureList();
            }
            catch (InvalidDataException)
            {
                MessageBox.Show("Invalid file format.", _windowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (EndOfStreamException)
            {
                MessageBox.Show("Corrupted file.", _windowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (NotSupportedException)
            {
                MessageBox.Show("Unsupported file.", _windowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, _windowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }