views:

625

answers:

2

I'm using XAML for serializing some objects and it's working great for the most part.

The problem I'm now facing is when I change the data structure all the old objects produce an exception like the one below. I don't mind if the values are lost.

Is there a way to turn off these exceptions and just have the xaml reader ignore unknown properties? If there isn't a way to do this now, is there maybe something in the new System.Xaml namespace that could do it?

System.Windows.Markup.XamlParseException: The property 'BorderPadding' does not exist in XML namespace 'clr-namespace:TemplateGenerator;assembly=App_Code'. Line '1' Position '158'.
  at System.Windows.Markup.XamlParser.ThrowExceptionWithLine(String message, Int32 lineNumber, Int32 linePosition)
  at System.Windows.Markup.XamlParser.ThrowException(String id, String value1, String value2, Int32 lineNumber, Int32 linePosition)
  at System.Windows.Markup.XamlParser.WriteUnknownAttribute(XamlUnknownAttributeNode xamlUnknownAttributeNode)
  at System.Windows.Markup.XamlParser.ProcessXamlNode(XamlNode xamlNode, Boolean& cleanup, Boolean& done)
  at System.Windows.Markup.XamlParser.ReadXaml(Boolean singleRecordMode)
  at System.Windows.Markup.TreeBuilderXamlTranslator._Parse()
  at System.Windows.Markup.XamlParser.Parse()
  at System.Windows.Markup.XamlTreeBuilder.ParseFragment()
  at System.Windows.Markup.TreeBuilder.Parse()
  at System.Windows.Markup.XamlReader.XmlTreeBuildDefault(ParserContext pc, XmlReader reader, Boolean wrapWithMarkupCompatReader, XamlParseMode parseMode, Boolean etwTracingEnabled)
  at System.Windows.Markup.XamlReader.Load(XmlReader reader)
  at System.Windows.Markup.XamlReader.Parse(String xamlText)
A: 

If you want your code to cope with the old attributes then you're going to have to trap the exception explicitly and then carry on reading the file.

By changing the data structure you've made the old XAML invalid and the parser is quite rightly objecting.

ChrisF
A: 

Looks like what I need to use is the DontThrowOnErrors flag in the new System.Xaml.XamlReaderSettings class in .net 4.0.

See http://msdn.microsoft.com/en-us/library/system.xaml.xamlreadersettings.dontthrowonerrors%28VS.100%29.aspx

Lone Coder
Apparently that property didn't make it into the release version of .net 4.0.
Lone Coder