xml Exception Error : x is an undeclared prefix

patrick

Well-known member
Joined
Dec 5, 2021
Messages
311
Programming Experience
1-3
Hello

xml Exception Error : x is an undeclared prefix
Please Help me

C#:
string sTemplate = @"
        <ControlTemplate x:Key=""Template"" TargetType=""MenuItem"">
            <Grid HorizontalAlignment=""Stretch"">                
                    <Rectangle Fill=""Red""/>              
            </Grid>
        </ControlTemplate>"
XamlReader.Load(new System.Xml.XmlTextReader(new StringReader(sTemplate))) as ControlTemplate;
 
You need to tell the XmlTextReader about the "x" namespace. Probabably embed that in the your sTemplate.

A few off-topic, but related things.
1) Don't use Hungarian naming convention in C# code. Hungarian notation was for people writing C and C++ code who had to debug at the kernel level and only had variable symbols to look at. Modern IDEs with their symbol lookup don't need Hungarian anymore, as well as the .NET naming convention explicitly tells people not to use Hungarian.
2) You should read the documentation. The documentation for XmlTextReader has a big note block telling you not to directly instantiate an instance of the class.
 
Back
Top Bottom