Example #1
0
        public static bool Convert(string sourceXmlFile, string destinationXmlFile, out string errorMessage, string rdmFieldDictFile = "./Dict/RDMFieldDictionary", string rdmEnumTypeDefFile = "./Dict/enumtype.def", bool verbose = false)
        {
            if (string.IsNullOrEmpty(sourceXmlFile) && string.IsNullOrEmpty(destinationXmlFile))
            {
                //Console.WriteLine($"{sourceXmlFile} {destinationXmlFile}");
                errorMessage = "Source and Destination file must not be empty path";
                return(false);
            }

            if (!File.Exists(sourceXmlFile))
            {
                errorMessage = $"Source file {sourceXmlFile} does not exists";
                return(false);
            }

            if (verbose)
            {
                Console.WriteLine($"Loading RDMFieldDictionary from {rdmFieldDictFile}");
            }
            errorMessage = string.Empty;
            if (!RdmUtils.LoadRdmDictionary(rdmFieldDictFile, out var fieldDictionary, out var errorMsg))
            {
                errorMessage = errorMsg;
                return(false);
            }
            if (verbose)
            {
                Console.WriteLine($"Loading RDMFieldDictionary completed.");
            }

            if (verbose)
            {
                Console.WriteLine($"\r\nLoading enumtype.def from {rdmEnumTypeDefFile}");
            }
            if (!RdmUtils.LoadEnumTypeDef(rdmEnumTypeDefFile, out var enumTypeDef, out errorMsg))
            {
                errorMessage = errorMsg;
                return(false);
            }
            if (verbose)
            {
                Console.WriteLine($"Loading enumtype.def completed.");
            }


            if (verbose)
            {
                Console.WriteLine($"Start reading and processing XML data from {sourceXmlFile}");
            }
            try
            {
                int numFragments = GetXmlFrgamentsCount(sourceXmlFile);

                var readerSettings =
                    new XmlReaderSettings()
                {
                    ConformanceLevel = ConformanceLevel.Fragment, Async = true
                };
                var writerSetting =
                    new XmlWriterSettings()
                {
                    ConformanceLevel = ConformanceLevel.Fragment, Async = true
                };
                using (var readerStream = File.OpenRead(sourceXmlFile))
                    using (var writerStream = File.Open(destinationXmlFile, FileMode.OpenOrCreate))
                        using (var xmlReader = XmlReader.Create(readerStream, readerSettings))
                            using (var xmlWriter = XmlWriter.Create(writerStream, writerSetting))
                            {
                                if (!DecodeAndCopy(xmlReader, xmlWriter, fieldDictionary, enumTypeDef, out errorMsg, numFragments, verbose))
                                {
                                    errorMessage = errorMsg;
                                    return(false);
                                }
                            }
            }
            catch (XmlException ex)
            {
                errorMessage = $"Error Detect!!: {ex.Message}";
                return(false);
            }
            return(true);
        }
        private async void XmlFragmentGrid1_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var xmlGridData = (XMLFragmentsData)XmlFragmentGrid1.SelectedItem;

            if (xmlGridData == null)
            {
                return;
            }
            _selectedIndex = xmlGridData.Index - 1;

            XmlTreeView.Items.Clear();

            //MessageBox.Show($"{_selectedIndex} {_fragmentCollection.Count}");
            if (_fragmentCollection.Count <= 0 || _selectedIndex < 0)
            {
                return;
            }

            if (!(_rdmFieldDictLoaded && _enumTypeDefDictLoaded))
            {
                Title = $"{_defaultTitle}::Loading RDMFieldDictioanary file";
                // Test Rdm dict
                var rdmResult = await RdmUtils.LoadRdmDictionaryAsync(_rdmDictFilePath);

                _fieldDictionary = rdmResult.Item2;
                if (!rdmResult.Item1)
                {
                    _rdmFieldDictLoaded = false;
                    MessageBox.Show(rdmResult.Item3);
                }

                Title = $"{_defaultTitle}::Loading enumtype.def file";
                var enumResult = await RdmUtils.LoadEnumTypeDefAsync(_enumTypeDefFilePath);

                _enumTypeDef = enumResult.Item2;
                if (!enumResult.Item1)
                {
                    _enumTypeDefDictLoaded = false;
                    MessageBox.Show(enumResult.Item3);
                }

                _rdmFieldDictLoaded    = true;
                _enumTypeDefDictLoaded = true;
            }

            Title = $"{_defaultTitle}::Building TreeView from selected XML Fragment";
            var settings = new XmlReaderSettings
            {
                ConformanceLevel = ConformanceLevel.Fragment, Async = true, IgnoreWhitespace = true
            };

            using (var reader = XmlReader.Create(new StringReader(_fragmentCollection[_selectedIndex].XmlRawData),
                                                 settings))
            {
                var tree = new TreeViewItem
                {
                    // Header =$"{_fragmentCollection[_selectedIndex].TimeStamp} {_fragmentCollection[_selectedIndex].MsgDirection}",
                    Header =
                        $"{XmlFragments.Get(_selectedIndex + 1).MsgTypeRawXmlData}\r\n{XmlFragments.Get(_selectedIndex + 1).TimeRawXmlData}\r\n{XmlFragments.Get(_selectedIndex + 1).RWFMajorMinorVersionRawXmlData}",
                    IsExpanded = true
                }; // instantiate TreeViewItem

                // assign name to TreeViewItem
                XmlTreeView.Items.Add(tree); // add TreeViewItem to TreeView
                await BuildTreeAsync(reader, tree).ConfigureAwait(false);
            }

            UnpackMrnBtn.IsEnabled  = !string.IsNullOrEmpty(_fragmentCollection[_selectedIndex].GUID);
            UnpackMrnBtn.Visibility = UnpackMrnBtn.IsEnabled ? Visibility.Visible : Visibility.Collapsed;

            XmlTreeView.Visibility = Visibility.Visible;
            XmlTreeView.IsEnabled  = true;

            Title = _defaultTitle;
        }