I'm saving a resource dictionary to Xaml
using XamlWriter
and I noticed that for string
properties with just {p}
, the XamlWriter
adds a {}
before the text (resulting in {}{p}
).
Why does that happen? Is there any way to prevent this?
Or at least to remove it safely when read back using a standard XDocument.Parse()
?
Here's how I'm saving it:
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);
Edit
The {}
is a scape sequence that is added when the text contains an open and close brackets like {...}
.
https://learn.microsoft.com/en-us/dotnet/desktop/xaml-services/escape-sequence-markup-extension
Now, the only question remaining is how can I safely remove these scape sequences from my text. Can I simply do a replace or trim, or is there a method for that already?