Using XamlFormatProvider
XamlFormatProvider makes it easy to import and export RadDocument to/from XAML format, preserving as much as possible of the document structure and formatting.
Since R2 2023 SP1 the XamlFormatProvider automatically verifies the imported XAML. More information is available in the Xaml Verification article.
To use XamlFormatProvider, you should reference the Telerik.WinControls.RichTextEditor.dll and add the following namespace:
- Telerik.WinForms.Documents.FormatProviders.Xaml
Import
In order to import a XAML document you can use the overloads of the Import() method.
The first example shows how to use XamlFormatProvider to import XAML document from a file.
Import XAML Document from a File
XamlFormatProvider xamlformatProvider = new XamlFormatProvider();
using (Stream stream = File.OpenRead(@"..\..\RichTextEditor\ImportExport\Sample.xaml"))
{
this.radRichTextEditor1.Document = xamlformatProvider.Import(stream);
}
Dim xamlformatProvider As XamlFormatProvider = New XamlFormatProvider()
Using inputStream As FileStream = File.OpenRead("..\..\RichTextEditor\ImportExport\Sample.xml")
Me.radRichTextEditor1.Document = xamlformatProvider.Import(inputStream)
End Using
Export
In order to export a document to XAML, you need to use the Export() method of XamlFormatProvider.
This example shows how to use XamlFormatProvider to export RadDocument to a file.
Export Document to a File
XamlFormatProvider provider = new XamlFormatProvider();
using (FileStream output = File.OpenWrite("Sample.xaml"))
{
RadDocument document = this.radRichTextEditor1.Document;
provider.Export(document, output);
}
Dim provider As XamlFormatProvider = New XamlFormatProvider()
Using output As FileStream = File.OpenWrite("Sample.xaml")
Dim document As RadDocument = Me.radRichTextEditor1.Document
provider.Export(document, output)
End Using