I have a string representation of a XAML Grid like this:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Canvas Background="Yellow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Label Content="textik" />
</Canvas>
</Grid>
What I need to do is create a Grid object out of this string. I tried a lot of approaches, but so far the closest is the code below:
string content = "<Grid xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Canvas Background=\"Yellow\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Label Content=\"textik\" /></Canvas></Grid>";
// the string is created programatically, I just put it here to see what it looks like at the end of the process
Stream stream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(content));
object objGrid = XamlReader.Load(stream);
Grid myGrid = (Grid) objGrid;
However, the XamlParsedException occurs saying that the root element is missing.
Do I have a mistake in a XAML code that I just cannot see? Or is the approach bad?
Thanks for an answer