我使用来自here的代码(来自Wiimax的答案)将我的FlowDocument转换为XML,然后再将其转换回FlowDocument。但现在我遇到了一些问题。
我的转换代码:
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;
}
}我输入以下代码(例如,它不是我在程序中使用的代码):

在转换之后,我得到了下面的代码:

您会看到一些空格被跳过。在命名空间中添加了{}之后,我查看了转换后的XML字符串,没有空格被跳过,但{}是他们的to
有没有人知道如何解决这个问题,或者看到我的失败?
发布于 2013-05-15 20:34:30
花括号:
这是因为绑定语法和WPF (Xamlreader和XamlWriter)想要帮助你;)。
在您的XAML中时,代码类似于
<Run Text="{" />Xaml-Engine首先假定一个绑定。由于没有,并且根据您创建FlowDocument的方式,'{'将转义为'{}{'
一种解决方法是,将花括号放在下面:
<Run>{</Run>另一种解决方法是避免大括号是第一个字符:
<Run Text=" {" />空格:有一个属性派上用场:
<Run xml:space="preserve">some Space</Run>这不是Xaml,而是一个由Xaml引擎处理的XML属性。
https://stackoverflow.com/questions/15615676
复制相似问题