Esempio n. 1
0
        /// <summary>
        /// Convert the Xml to Rtf with some formats specified in the XMLViewerSettings,
        /// and then set the Rtf property to the value.
        /// </summary>
        public void Process()
        {
            if (string.IsNullOrWhiteSpace(this.Text))
            {
                return;
            }

            _inProcessing     = true;
            CurrentParseError = null;

            try
            {
                // maintain the position of the cursor
                var pos = SelectionStart;

                // The Rtf contains 2 parts, header and content. The colortbl is a part of
                // the header, and the {1} will be replaced with the content.

                // Get the XDocument from the Text property.
                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(Text);

                StringBuilder xmlRtfContent = new StringBuilder();

                // Get the Rtf of the root element.
                string rootRtfContent = ProcessElement(xmlDoc.DocumentElement, 0);

                xmlRtfContent.Append(rootRtfContent);

                // Construct the completed Rtf, and set the Rtf property to this value.
                Rtf = string.Format(RtfFormat, Settings.ToRtfFormatString(), xmlRtfContent.ToString());

                // put the cursor back in the correct spot
                SelectionStart = pos;

                // done processing
                NotificationMessage?.Invoke(this,
                                            new NotificationEventArgs($@"Input Xml processed: {xmlDoc.DocumentElement.ToString()}",
                                                                      MessageLevel.Information));

                // allow the Text property change to call proces
                _handleKeyPress = true;
            }
            catch (XmlException xmlException)
            {
                NotificationMessage?.Invoke(this,
                                            new NotificationEventArgs($@"Please check the input Xml.Error: {xmlException.Message}",
                                                                      MessageLevel.Warning,
                                                                      xmlException));

                CurrentParseError = xmlException;
            }
            catch (Exception ex)
            {
                NotificationMessage?.Invoke(this,
                                            new NotificationEventArgs($@"An unknown exception has occurred: {ex.Message}",
                                                                      MessageLevel.Exception,
                                                                      ex));

                CurrentParseError = ex;
            }
            finally
            {
                // done processing!
                _inProcessing = false;
            }
        }