title: Exception while rendering XpsDocument after conversion from FlowDocument
body: <p>I'm developing an application that heavily uses the conversion from a FlowDocument to a XpsDocument. From time to time an exception is thrown:
  NotSupportedException: "The URI prefix is not recognized."</p>
  
  <p>My scenario is very complex and I condensed it to a simple test. If you type in some characters in the text box (fast), you'll get the exception immediately.</p>
  
  <p>Xaml:</p>
  
  <pre><code>&lt;Window x:Class="FlowDocPreview.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition /&gt;
              &lt;ColumnDefinition /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;RichTextBox Name="rtb" KeyUp="rtb_KeyUp" /&gt;
          &lt;DocumentViewer Grid.Column="1" Name="dv" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>using System;
  using System.IO;
  using System.IO.Packaging;
  using System.Windows;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Xps;
  using System.Windows.Xps.Packaging;
  
  
  namespace FlowDocPreview
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              rtb.Document = new FlowDocument();
          }
  
          Uri _packageUri = null;
  
          private void rtb_KeyUp(object sender, KeyEventArgs e)
          {
              using (MemoryStream ms = new MemoryStream())
              {
                  dv.Document = null;
                  if (_packageUri != null)
                  {
                      PackageStore.RemovePackage(_packageUri);
                  }
  
                  _packageUri = new Uri("memorystream://" + DateTime.Now.Ticks + ".xps");
  
                  TextRange sourceContent = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
                  MemoryStream stream = new MemoryStream();
                  sourceContent.Save(stream, DataFormats.Xaml);
                  FlowDocument flowDocumentCopy = new FlowDocument();
                  TextRange copyDocumentRange = new TextRange(flowDocumentCopy.ContentStart, flowDocumentCopy.ContentEnd);
                  copyDocumentRange.Load(stream, DataFormats.Xaml);
  
                  Package package = Package.Open(ms, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                  PackageStore.AddPackage(_packageUri, package);
                  XpsDocument xpsDoc = new XpsDocument(package, CompressionOption.SuperFast);
                  xpsDoc.Uri = _packageUri;
                  XpsDocumentWriter documentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
                  documentWriter.Write(((IDocumentPaginatorSource)flowDocumentCopy).DocumentPaginator);
  
                  dv.Document = xpsDoc.GetFixedDocumentSequence();
              }
          }
      }
  }
  </code></pre>
  
  <p>That exception cannot be catched (refer to <a href="https://social.msdn.microsoft.com/Forums/windows/en-US/431e8e80-3bf0-4679-a0c0-9b5cae4f2f38/systemnotsupportexception-the-uri-prefix-is-not-recognized-when-creating-xps-document?forum=netfxbcl" rel="nofollow">https://social.msdn.microsoft.com/Forums/windows/en-US/431e8e80-3bf0-4679-a0c0-9b5cae4f2f38/systemnotsupportexception-the-uri-prefix-is-not-recognized-when-creating-xps-document?forum=netfxbcl</a> ) or prevented in advance (the package seems to be existing correctly). I examined the .net code and found no reason for that strange behaviour. In total I've spent day or even weeks to find a fix.</p>
  
  <p>Any ideas? Regards, Heady</p>