This is a migrated thread and some comments may be shown as answers.

Try Catch XML Import for RadRichText

3 Answers 159 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Cuthahotha
Top achievements
Rank 1
Cuthahotha asked on 22 Nov 2010, 03:35 PM
The following code doesn't drop into the catch block on an error in the xml.  If I have a string "Test" in my content, the code errors on the italic line below with this error.   And will not drop in to the catch block. 

{System.Windows.Markup.XamlParseException: illegal xml character [Line: 1 Position: 2]
   at MS.Internal.XcpImports.CreateFromXaml(String xamlString, Boolean createNamescope, Boolean requireDefaultNamespace, Boolean allowEventHandlers, Boolean expandTemplatesDuringParse)
   at MS.Internal.XcpImports.CreateFromXaml(String xamlString, Boolean createNamescope, Boolean requireDefaultNamespace, Boolean allowEventHandlers)
   at System.Windows.Markup.XamlReader.Load(String xaml)
   at Telerik.Windows.Documents.FormatProviders.Xaml.XamlFormatProvider.Import(String input)
   at Telerik.Windows.Documents.FormatProviders.Xaml.XamlFormatProvider.Import(Stream input)
   at ICollegeLifeSLClient.Classes.RadDocSupport.ImportXaml(String content)}

public RadDocument ImportXaml(string content)
  {
      try
      {
          IDocumentFormatProvider provider = new XamlFormatProvider();
          RadDocument document;
          using (MemoryStream stream = new MemoryStream())
          {
              StreamWriter writer = new StreamWriter(stream);
              writer.Write(content);
              writer.Flush();
              stream.Seek(0, SeekOrigin.Begin);
              document = provider.Import(stream);
          }
          return document;
      }
      catch (Exception ex)
      {
          string message = ex.Message;
          return null;
      }
  }

3 Answers, 1 is accepted

Sort by
0
Ivailo Karamanolev
Telerik team
answered on 23 Nov 2010, 08:56 AM
Hi Cuthahotha,

I have rewritten the method you use with the much more concise Import(string) methods and couldn't reproduce the issue, please try that. If your issues persist, make sure you have referenced the latest versions of the binaries, and if still - contact us, preferably with a demo project, for further assistance. Here's the modified version of the method:
private RadDocument Import(string str)
{
    try
    {
        return new XamlFormatProvider().Import(str);
    }
    catch (Exception ex)
    {
        return null;
    }
}


Best wishes,
Ivailo
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
caleb
Top achievements
Rank 1
answered on 04 Aug 2016, 06:09 AM
Hello there,

 

I was able to reproduce this. 

Input (m.Text):

Z6p00b40OIP5D7ES2RritoCu2T0N22nxMCLxhXNeJMW5BL0gCBQNZeHKDwQyn7BPT+7hlNR0fRqnCINEAAboOueS0JUer0mHd4yfXDNeoKyXQWp9kWFW4qcSPL2TjWTe5mW4SrK4ir/URMuXu2rpl9ttpNjcwHX1fajGviZJnpKJIKpt76DF75btS34V10nzpUWBIqklAVsDQEDVb+nFvNe3d28oSU0KHYEeS6t2LpRylD6PrKUghHvS6rrCdXKCRMRp0RmtE2WSrOEmkcDcf0YYENiPyOsWZ040uCOgttj+Hjlatz3woIRd8F6OvP6BwB5OUcSB7xBCq09qMGjAyK6VfGcFP6UHUYYpNOwPPeJEPL8Y4LjvmfGjohzdBm/4

Code:

 
if (m.Text.Length > 100 && !m.Text.Substring(0, 100).Contains(" "))
{
    var document = new RadDocument();
    var bytes = Convert.FromBase64String(m.Text);
    try
    {
        document = new XamlFormatProvider().Import(bytes);
        //Now dump out the rich text format and save
        var customFormatProvider = new TxtFormatProvider();
        var txtText = customFormatProvider.Export(document);
 
        txtText = g.ReplaceQuotes(txtText);
        m.TXTText = txtText;
        DatabaseConnection.Update(m); //Do not update if an error has occured
    }
    catch (Exception err)
    {
        LogHelper.CreateLog("CleanupData.cs: Populating TXTText", err);
    }
 }
0
Todor
Telerik team
answered on 08 Aug 2016, 09:03 AM
Hello Caleb,

Thank you for the sent code-snippet.

I've tried to convert the encoded Base64 string to a string which describes a valid XML document, but to no avail. Indeed, this is the reason the document cannot be imported. Can you share how the RadDocument instance is exported and the result string is achieved?

Looking forward to your reply.

Regards,
Todor
Telerik by Progress
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
RichTextBox
Asked by
Cuthahotha
Top achievements
Rank 1
Answers by
Ivailo Karamanolev
Telerik team
caleb
Top achievements
Rank 1
Todor
Telerik team
Share this question
or