I am creating a formatted FlowDocumentfrom XML. The XML is well-formed and consists mainly of 10,000 nodes, each with one node with a 6-digit string value.
XML parsing XElementand building FlowDocumentin memory takes about 5 seconds. Assigning a FlowDocumentproperty Documentto for RichTextBoxin my application takes about 7 minutes and allocates CPU for that time.
Here is the relevant code snippet:
// The following six lines of code execute in about 5 seconds
var xml = XElement.Parse(response.Data);
PrettyXmlConverter px = new PrettyXmlConverter();
FlowDocument fd = px.Render(xml);
Paragraph p = new Paragraph();
p.Inlines.Add(new Run(response.TimeStamp.ToShortDateString() + " " + response.TimeStamp.ToLongTimeString()));
fd.Blocks.InsertBefore(fd.Blocks.ElementAt(0), p);
// This line of code takes about 7 minutes and maxes out the CPU for that time.
tbResponse.Document = fd;
I wonder what is going on here. I profiled the code and saw tens of thousands of calls to unmanaged methods, such as fsFormatSubtrackBottomlessand SubtrackFormatParaBottomless.
Can someone shed some light on the problem or come up with a workaround?