tags:

views:

38

answers:

2

How do I make XamlReader.Load to just ignore unknown attributes and elements instead of throwing exceptions? It would be much more useful if it only ignored those.

+1  A: 

You can't, XamLReader.Load requires the document to be well-formed. This means:

  • The XAML content string must define a single root element.

  • The content string XAML must be well formed XML, as well as being parseable XAML.

  • The required root element must also specify a default XML namespace value. This is typically the Silverlight namespace, xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation". This XML namespace is required explicitly in Silverlight 2 and onward whereas it was implicitly assumed in Silverlight 1.0 and its CreateFromXaml JavaScript method.

more information can bei found in the msdn.

Femaref
A: 

You can't make it ignore unknown attributes and elements. If you need to stick attributes and elements into your XAML for reasons other than deserialization, put them in their own namespace, e.g.:

<Window x:Class="MyApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="my"
    xmlns:my="my-namespace"
    Title="MainWindow" Height="350" Width="525">
  <StackPanel my:Attribute="The XamlReader will ignore this.">
    <my:Element>It will ignore this, too.</my:Element>
  </StackPanel>
</Window>

Note that you have to use the Markup Compatibility namespace and add your namespace prefix to its Ignorable attribute in order to get the XamlReader to ignore your namespace instead of throwing an exception. See the mc:Ignorable documentation for full details.

Robert Rossney