WPF和C#中的RTF编辑器,可以打开和保存html格式文件 [英] rich text editor in WPF and C# which can open and save html format file

查看:587
本文介绍了WPF和C#中的RTF编辑器,可以打开和保存html格式文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用C#和wpf创建了文本编辑器,但是它们仅以RTF格式保存文件..我想将文件保存为.html格式..
有microsoft.mshtml.dll DLL可以使用..但是我不知道如何使用它并创建这样的编辑器,以html格式打开和保存文档..请帮助我..
我将富文本编辑器的代码放在这里..
请帮我...

I have created the text editor in C# and wpf but which save the file only in rich text format..i want to save the file in .html format..
there is microsoft.mshtml.dll DLL to used..but i dont know how to used this and create such editor which open and save document in html format..please help me..
i am putting my code of rich text editor here..
please help me...

void CmdNew(object sender, RoutedEventArgs e)
       {
           txtcreatelssn.Document = new FlowDocument();
       }
       void Open1(object sender, RoutedEventArgs e)
       {
           System.Windows.Forms.OpenFileDialog openFile = new System.Windows.Forms.OpenFileDialog();
           openFile.Filter = "XAML Files (*.xaml)|*.xaml|RichText Files (*.rtf)|*.rtf|All Files (*.*)|*.*";

           if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               // Create a TextRange around the entire document.
               TextRange documentTextRange = new TextRange(txtcreatelssn.Document.ContentStart, txtcreatelssn.Document.ContentEnd);

               using (FileStream fs = File.Open(openFile.FileName, FileMode.Open))
               {
                   if (System.IO.Path.GetExtension(openFile.FileName).ToLower() == ".rtf")
                   {
                       documentTextRange.Load(fs, System.Windows.DataFormats.Rtf);
                   }
                   else
                   {
                       documentTextRange.Load(fs, System.Windows.DataFormats.Xaml);
                   }
               }

           }
       }

       void save1(object sender, RoutedEventArgs e)
       {
           System.Windows.Forms.SaveFileDialog savefile = new System.Windows.Forms.SaveFileDialog();
           //System.Windows.Forms.OpenFileDialog openFile = new System.Windows.Forms.OpenFileDialog();
           savefile.Filter = "XAML Files (*.xaml)|*.xaml|RichText Files (*.rtf)|*.rtf|All Files (*.*)|*.*";

           if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               // Create a TextRange around the entire document.
               TextRange documentTextRange = new TextRange(
                   txtcreatelssn.Document.ContentStart, txtcreatelssn.Document.ContentEnd);
               MemoryStream ms = new MemoryStream();
               // If this file exists, it''s overwritten.
               using (FileStream fs = File.Create(savefile.FileName))
               {
                   if (System.IO.Path.GetExtension(savefile.FileName).ToLower() == ".rtf")
                   {
                       documentTextRange.Save(fs,System.Windows.DataFormats.Rtf);
                   }
                   else
                   {
                       documentTextRange.Save(fs, System.Windows.DataFormats.Xaml);

                   }
               }

           }

       }

推荐答案

您可能希望阅读 ^ ]博客文章,其中详细介绍了如何使用支持XAML到HTML和HTML到XAML的数据绑定RichTextBox.

基本上,它是通过将HTML(在可能的情况下)转换为XAML来工作的.表面上HTML类似于XML,但事实并非如此-唯一真正的相似之处在于它们都支持标签和<>人物.由于HTML在格式上不如XML严格,因此无法使用XSL进行转换.
You may want to have a read of this[^] blog post from Michael Sync which details how to work with a databound RichTextBox that supports XAML to HTML and HTML to XAML.

Basically, it works by converting the HTML (where it can) into XAML. Superficially HTML resembles XML, but it''s not - the only real resemblance is that they both support tags and the <> characters. As HTML is not as strict in its well-formed nature as XML, it cannot be converted using XSL.


这篇关于WPF和C#中的RTF编辑器,可以打开和保存html格式文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆