} // ToTextButtonClick // ---------------------------------------------------------------------- private void ToXmlButtonClick( object sender, RoutedEventArgs e ) { try { TextRange conversionText = ConversionText; using ( MemoryStream stream = new MemoryStream() ) { conversionText.Save( stream, DataFormats.Rtf ); stream.Seek( 0, SeekOrigin.Begin ); using ( StreamReader reader = new StreamReader( stream ) ) { IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc( reader ); XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.Indent = true; xmlWriterSettings.IndentChars = ( " " ); StringBuilder sb = new StringBuilder(); using ( XmlWriter writer = XmlWriter.Create( sb, xmlWriterSettings ) ) { RtfXmlConverter xmlConverter = new RtfXmlConverter( rtfDocument, writer ); xmlConverter.Convert(); writer.Flush(); this.textBox.Text = sb.ToString(); } } } } catch ( Exception exception ) { MessageBox.Show( this, "Error " + exception.Message, this.Title, MessageBoxButton.OK, MessageBoxImage.Error ); } } // ToXmlButtonClick
// ---------------------------------------------------------------------- private void ToXmlButtonClick( object sender, EventArgs e ) { try { IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc( ConversionText ); XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.Indent = true; xmlWriterSettings.IndentChars = ( " " ); StringBuilder sb = new StringBuilder(); using ( XmlWriter writer = XmlWriter.Create( sb, xmlWriterSettings ) ) { RtfXmlConverter xmlConverter = new RtfXmlConverter( rtfDocument, writer ); xmlConverter.Convert(); writer.Flush(); textBox.Text = sb.ToString(); } } catch ( Exception exception ) { MessageBox.Show( this, "Error " + exception.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error ); } }
// ---------------------------------------------------------------------- private void ConvertXml( IRtfDocument rtfDocument ) { try { XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.Indent = true; xmlWriterSettings.IndentChars = ( " " ); xmlWriterSettings.Encoding = settings.Encoding; string fileName = settings.BuildDestinationFileName( null, RtfXmlConverter.DefaultXmlFileExtension ); using ( XmlWriter writer = XmlWriter.Create( fileName, xmlWriterSettings ) ) { RtfXmlConvertSettings xmlConvertSettings = new RtfXmlConvertSettings(); xmlConvertSettings.Prefix = settings.XmlPrefix; xmlConvertSettings.Ns = settings.XmlNamespace; xmlConvertSettings.IsShowHiddenText = settings.ShowHiddenText; RtfXmlConverter xmlConverter = new RtfXmlConverter( rtfDocument, writer, xmlConvertSettings ); xmlConverter.Convert(); writer.Flush(); } } catch ( Exception e ) { Console.WriteLine( "error while converting to xml: " + e.Message ); ExitCode = ProgramExitCode.ConvertXml; } }