I am trying to save a collection of objects with XamlWriter
in the simplest way possible. For some reason saving them as an array produces invalid XML:
var array = new int[] {1, 2, 3};
Console.Write(XamlWriter.Save(array));
outputs:
<Int32[] xmlns="clr-namespace:System;assembly=mscorlib">
<Int32>1</Int32>
<Int32>2</Int32>
<Int32>3</Int32>
</Int32[]>
Attempting to read this using XamlReader
throws:
The '[' character, hexadecimal value 0x5B, cannot be included in a name. Line 1, position 7
I've tried saving as a List<T>
instead but I get the usual XAML generics error. Is there some simple way I can do it (preferably with LINQ) or do I have to define my own wrapper type?
XamlWriter.Save
produces invalid XML.
<Int32[] xmlns="clr-namespace:System;assembly=mscorlib">
<Int32>1</Int32>
<Int32>2</Int32>
<Int32>3</Int32>
</Int32[]>
I don't know the reasons behind that, but using XamlServices.Save
seems to fix the problem.
<x:Array Type="x:Int32" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:Int32>1</x:Int32>
<x:Int32>2</x:Int32>
<x:Int32>3</x:Int32>
</x:Array>
Additional notes from MSDN
The following classes exist in both the WPF assemblies and the
System.Xaml
assembly in the .NET Framework 4:
XamlReader
XamlWriter
XamlParseException
The WPF implementation is found in the
System.Windows.Markup
namespace, andPresentationFramework
assembly.The
System.Xaml
implementation is found in theSystem.Xaml
namespace.If you are using WPF types or are deriving from WPF types, you should typically use the WPF implementations of
XamlReader
andXamlWriter
instead of theSystem.Xaml
implementations.For more information, see Remarks in
System.Windows.Markup.XamlReader
andSystem.Windows.Markup.XamlWriter
.