0

I am trying to Parse a XML file using the XamlReader which has the following contents

    <Canvas xmlns:seic="clr-namespace:NameSpace1;assembly=Assembly1" 
    xmlns:seidm="clr-namespace:NameSpace2;assembly=Assembly2" 
    xmlns:seogptpi="clr-namespace:NameSpace3;assembly=Assembly3" 
    xmlns:seogdc="clr-NameSpace4;assembly=Assembly4" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Name="_99abfc59e3ad417d98db31591a6f9dd7" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    Canvas.Left="0" Canvas.Top="0" Tag="TemplatePart" Width="800" Height="600"  
    seogdc:FrameworkElementExtensions.Id="Canvas" 
    seogdc:FrameworkElementExtensions.ShowRotateCue="False" 
    seogdc:FrameworkElementExtensions.ShowPositionCue="False" 
    seogdc:FrameworkElementExtensions.IsSizeSerializable="False" 
    seogdc:FrameworkElementExtensions.IsTargetDimensions="False">
<Canvas x:Name="Canvas1" 
      Canvas.Left="0"    
      Canvas.Top="0" 
      Tag="TemplatePart" 
      Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type Canvas}}, Path=Width}" 
      Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type Canvas}}, Path=Height}" 
      Background="{x:Static Brushes.Black}"
      seogdc:FrameworkElementExtensions.Id="Canvas" 
      seogdc:FrameworkElementExtensions.ShowRotateCue="False" 
      seogdc:FrameworkElementExtensions.ShowPositionCue="False" 
      seogdc:FrameworkElementExtensions.IsTargetDimensions="False" 
      seogptpi:TemplateManager.IsMainPart="True"
      seogptpi:TemplateManager.TemplateName="Canvas">
<seic:DomainParameter.DomainModel>
  <seidm:StatelessDomainModel>
    <seidm:StatelessDomainModel.DomainFeatures>
      <seidm:ColorFeature LabelFillTransparency="0">
        <seidm:ColorFeature.FontBlinkSetting>
          <seidm:ColorTransparancy Transparancy="0" />
        </seidm:ColorFeature.FontBlinkSetting>
        <seidm:ColorFeature.LineBlinkSetting>
          <seidm:ColorTransparancy Transparancy="0" />
        </seidm:ColorFeature.LineBlinkSetting>
        <seidm:ColorFeature.FillBlinkSetting>
          <seidm:ColorTransparancy Transparancy="0" />
        </seidm:ColorFeature.FillBlinkSetting>
      </seidm:ColorFeature>
    </seidm:StatelessDomainModel.DomainFeatures>
  </seidm:StatelessDomainModel>
</seic:DomainParameter.DomainModel>
  </Canvas>
</Canvas>

But it simply throws an XamlParseException('Cannot set unknown member 'Namespace2.ColorFeature.LabelFillTransparency'.' Line number '13' and line position '31'.) all the time. I have added references to the 4 assemblies mentioned above. For privacy purposes i have chosen not to provide the exact assembly names. Can someone tell me what might be happening?

Thanks in advance.

Anee
  • 463
  • 9
  • 26
  • Looking at no responses i feel i might have asked a silly question. I am stuck with this problem for quite sometime now. I really hope someone helps me in figuring out what is going wrong here. – Anee Aug 28 '13 at 16:14

1 Answers1

0

You need to provide a custom ParserContext to the XamlReader.Parse method. You should assign the ParserContext.XamlTypeMapper property to enable XamlParser to locate the types referenced in your XAML. Something along these lines:

var context = new ParserContext();
context.XamlTypeMapper = new XamlTypeMapper(
  new[] {
    Assembly.GetExecutingAssembly().GetName().Name,
    "Assembly1",
    "Assembly2",
    "Assembly3",
    "Assembly4",
  }
);
var element = (FrameworkElement) XamlReader.Parse(xaml, context);
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • I tried the solution you asked me to and its still not able to find the types mentioned in the xaml. I tried giving the assembly paths also using the XamlTypeMapper. But still no luck:( I was very hopeful when i saw your reply but to my bad luck even this doesn't work.. – Anee Sep 11 '13 at 06:33
  • @Anee: Unless your XAML has errors you should be able to parse it if you provide the proper `ParserContext`. The error you get is not about a missing type but an unknown member and that can perhaps help you troubleshoot your problem. Can you instantiate a `ColorFeature` and access `LabelFillTransparency` from code? – Martin Liversage Sep 11 '13 at 06:52
  • Hi, I am able to create the above mentioned types in the code perfectly without any problem. But when i try to load the above XAML it doesn't work. – Anee Sep 11 '13 at 07:09
  • I got it. When i checked the code i found that the class name is "ColorTransparency" and not "ColorTransparancy". But i must say, its your tips which gave me the breakthrough. I really appreciate it. Thanks a lot Martin. – Anee Sep 11 '13 at 08:29
  • Looks like i have one more problem. In the above XAML i have seidm:StatelessDomainModel which implements the IDomainModel interface and the seic:DomainParameter.DomainModel is a dependency property of type IDomainModel whose definition is as follows public static readonly DependencyProperty DomainModelProperty = DependencyProperty.RegisterAttached("DomainModel", typeof(IDomainModel), typeof(DomainParameter), new UIPropertyMetadata(null)); When i try parsing the XAML i get the below expection "StatelessDomainModel' is not a valid value for property 'DomainModel'".Why? – Anee Sep 12 '13 at 06:21