2

I'm serializing a object via XamlWriter.Save(object). This works as expected.

Problem now is, XamlWriter sets the namespaces as it needs, for example the root Namespace xmlns is set to the namespace of my object.

Problem is, I want to copy the xaml into a existing one, where already namespaces are defined. It works, because the XamlWriter also adds the namespace attributes to the generated string, but I want that my already existing prefixes will be reused. Is this possible?

Sample XAML:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:assembly="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:shared="urn:mlog-sharedclasses-ns"
      xmlns:visu="urn:mlog-visucommon-ns"
      Width="2000"
      Height="1000"
      Background="White"
      TextBlock.FontFamily="{StaticResource OpenSans}"
      assembly:Name="rootElement"
      mc:Ignorable="d">
  <Grid.Resources>
    <BoolToBrushConverter xmlns="clr-namespace:ControlPanel.SharedClasses.Converters;assembly=MCC.ControlPanel.SharedClasses"
                          FalseValue="#FF00FFFF"
                          TrueValue="#FF0000FF"
                          assembly:Key="ID_880cf_BorderBrush_Converter" />
  </Grid.Resources>
  <visu:TagRootCanvas>
    <Button Content="Button"
            Width="412"
            Height="231"
            Canvas.Left="33"
            Canvas.Top="71"
            assembly:Name="ID_880cf">
      <i:Interaction.Behaviors>
        <visu:TagBindingBehavior xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                 xmlns="urn:mlog-visucommon-ns"
                                 Converter="{av:StaticResource ID_880cf_BorderBrush_Converter}"
                                 Debug="False"
                                 Mode="OneWay"
                                 Property="{shared:Static Member=av:Border.BorderBrushProperty}"
                                 TagDataType="Bool"
                                 TagName="aaa"
                                 UpdateSourceTrigger="Default"
                                 UseConvertedNullAsFallback="False" />
      </i:Interaction.Behaviors>
    </Button>
  </visu:TagRootCanvas>
</Grid>

I create the TagBindingBehavior later via XamlWriter.Save and add it to my document. But it has different Namespaces. I want to remove the "xmlns" attributes from TagBindingBehavior.

Jochen Kühner
  • 1,385
  • 2
  • 18
  • 43
  • Have a look at existing [limitations](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/serialization-limitations-of-xamlwriter-save#serialization-is-self-contained). I think that the point is here _everything that is serialized is contained inside a XAML single page, with a single root element, and no external references other than URIs._ – Pavel Anikhouski Dec 14 '19 at 10:24
  • Also could you please share the sample, which object are you saving and how? There is only xaml code in question – Pavel Anikhouski Dec 14 '19 at 10:25
  • @codebender Have a look at Scott Hanselman's [blog](https://www.hanselman.com/blog/XmlFragmentWriterOmitingTheXmlDeclarationAndTheXSDAndXSINamespaces.aspx). You could extend (re)usability by ctor injecting a list of `localName`s to omit and a list of `( prefix, localName, ns )` types to add namespace declarations. – Funk Dec 14 '19 at 12:19

1 Answers1

0

I don't follow the explanation fully but maybe that doesn't really matter. The result isn't what you want somehow.

If you use as stringwriter parameter with xamlwriter.save you can write your xaml to a string in memory rather than directly to disk. This is both a string and xml. You can parse that to an xml document and manipulate it or you could just change the string with string.replace() or the like. Either way, you can change any attribute or property you like.

Andy
  • 11,864
  • 2
  • 17
  • 20
  • I know that, I already write into a string not into a File. Problem is XAML is not XML. You also have MarkupExtensions, wich XML does not know. So if I change a xmlns maybe also a MarkupExtension is affected – Jochen Kühner Nov 23 '18 at 19:03