Reputation: 1332
I use the Code from here(Answer from Wiimax) to convert my FlowDocument
to XML
and convert it back to FlowDocument
. But now i have some Problems here.
My Code for the convert:
public static bool IsFlowDocument(this string xamlString)
{
if (xamlString == null || xamlString == "")
throw new ArgumentNullException();
if (xamlString.StartsWith("<") && xamlString.EndsWith(">"))
{
XmlDocument xml = new XmlDocument();
try
{
xml.LoadXml(string.Format("<Root>{0}</Root>", xamlString));
return true;
}
catch (XmlException)
{
return false;
}
}
return false;
}
public static FlowDocument toFlowDocument(this string xamlString)
{
if (IsFlowDocument(xamlString))
{
var stringReader = new StringReader(xamlString);
var xmlReader = System.Xml.XmlReader.Create(stringReader);
return XamlReader.Load(xmlReader) as FlowDocument;
}
else
{
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(new Run(xamlString));
FlowDocument myFlowDocument = new FlowDocument();
myFlowDocument.Blocks.Add(myParagraph);
return myFlowDocument;
}
}
I enter this code(as example, its not the code, which i use in my program):
And after convert i get back this code:
You see that some blank were skipped. And after the namespace there was add a { } I looked at the converted XML String, there are no blank skipped, but the { } is their to
Do someone know how to fix that or see a fail from me?
Upvotes: 2
Views: 4399
Reputation: 359
curly Braces:
this is because of Binding-Syntax and WPF (Xamlreader and XamlWriter) wants to help you ;).
When in your XAML-Code is something like
<Run Text="{" />
the Xaml-Engine first assumes a binding. Since there is no and depending on how you create your FlowDocument the '{' is Escaped to '{}{'
One Workaround is, that you put your curly brace like this:
<Run>{</Run>
another workaround is to avoid that the curly brace is the first character:
<Run Text=" {" />
spaces: There is an attribute which comes in handy:
<Run xml:space="preserve">some Space</Run>
this isn't a Xaml but an XML attribute which is handled by the Xaml-Engine.
Upvotes: 1