S3ddi9
S3ddi9

Reputation: 2151

Can't catch System.Xaml.XamlParseException

I have an empty string, when I use it on an XmlReader it gives of course a parsing "root element is missin" exception, am trying to catch it but the try,catch doesn't respond, is there a way to catch this exception or detect that my string is not parsable.

System.IO.StringReader stringReader = new System.IO.StringReader("");

System.Xml.XmlReader xmlReader = System.Xml.XmlTextReader.Create(stringReader, new System.Xml.XmlReaderSettings());

try
{
    object ob = System.Windows.Markup.XamlReader.Load(xmlReader);//
    mycv = (Canvas)ob;
}
    catch (Exception ex) //even if I use System.Xaml.XamlParseException
{
    mycv = new Canvas();
}

Upvotes: 2

Views: 1549

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81243

object ob = System.Windows.Markup.XamlReader.Load(xmlReader);

Here you are using Windows markup XamlReader, so System.Xaml.XamlParseException won't get thrown here instead you should catch System.Windows.Markup.XamlParseException.

This should work for you -

try
{
    object ob = System.Windows.Markup.XamlReader.Load(xmlReader);//
    mycv = (Canvas)ob;
}
    catch (System.Windows.Markup.XamlParseException ex)
{
    mycv = new Canvas();
}

Upvotes: 3

Related Questions