我正在保存一个资源字典来Xaml
使用XamlWriter
,我注意到对于string
只有 的属性{p}
,在文本之前XamlWriter
添加了 a {}
(导致{}{p}
)。
为什么会这样?有什么办法可以防止这种情况发生吗?或者至少在使用标准回读时安全地删除它XDocument.Parse()
?
这是我保存它的方法:
public class Example
{
public string Property { get; set; }
}
var resource = new ResourceDictionary();
resource.Add("Example", new Example { Property = "{p}" });
var settings = new XmlWriterSettings
{
Indent = true,
IndentChars = "\t",
OmitXmlDeclaration = true,
CheckCharacters = true,
CloseOutput = true,
ConformanceLevel = ConformanceLevel.Fragment,
Encoding = Encoding.UTF8,
};
using (var fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None))
using (var writer = XmlWriter.Create(fileStream, settings))
XamlWriter.Save(resource, writer);
编辑
{}
是当文本包含左括号和右括号(如{...}
.
https://docs.microsoft.com/en-us/dotnet/desktop/xaml-services/escape-sequence-markup-extension
现在,剩下的唯一问题是如何安全地从文本中删除这些 scape 序列。我可以简单地进行替换或修剪,还是已经有方法了?