-1

I found a very strange issue when to use XamlReader to load the menu of my app from a predefined xaml file. I need to define the attribute of xml:space="preserve" and the xaml is like this:

<m:MenuManager>
   ...
    <m:Resource>
        <m:Resource.ResourceTitle>
            <sys:String xml:space="preserve">Click the Button&#xA;(InvokeCommandAction)&#xA;View</sys:String>
        </m:Resource.ResourceTitle>
    </m:Resource>
...
</m:MenuManager>

Load the xaml content to the string and use XamlReader.Load to convert it to the MenuManager object. When at the first time the XamlReader.Load method is called, it will only return the words within the tag <sys:String xml:space="preserve"> , and the expected result only returned at the second time.

        var uri = new Uri("/Sample;component/Assets/Menu.xaml", UriKind.Relative);
        var info = Application.GetResourceStream(uri);
        string xaml = null;
        using (StreamReader reader = new StreamReader(info.Stream))
        {
            xaml = reader.ReadToEnd();
        }
        //when the first time load, only a string value of
        //"Click the Button&#xA;(InvokeCommandAction)&#xA;View" is returned
        var temp1 = XamlReader.Load(xaml); 

        //when the second time load, all menu content loaded successfully and
        //converted to the object of MenuManager 
        readXaml = XamlReader.Load(xaml) as MenuManager;

When I remove the attribute xml:space="preserve" or change it to xml:space="default" it will work fine and I can get the object of MenuManager by calling the method XamlReader.Load only once.But I really need keep the whitespace on my page, and the code here looks so strange. Can anybody please explain this? Thanks!

yihao
  • 11
  • 2
  • also reported as a bug https://connect.microsoft.com/VisualStudio/feedback/details/615562/xml-space-preserve-goes-out-of-scope-when-used-twice-in-nested-elements maybe used internally ,so can be reserved a word. – Davut Gürbüz May 07 '12 at 11:09
  • yes, i upgrade the sl version from 4 to 5, and it was solved – yihao May 14 '12 at 05:24

1 Answers1

1

if you dont want to add xml:space="preserve" to all elements, you could use this additional parameter to XamlReader.Load :

XamlReader.Load(xaml, new ParserContext() { XmlSpace = "preserve" });
slaesh
  • 16,659
  • 6
  • 50
  • 52