.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Post New Web Links

XAML Serialization Problem

Posted By:      Posted Date: October 07, 2010    Points: 0   Category :WPF

I try to save (serialize) a XAML document. The background is that the user of the application should be able to change the XAML document dynamically.

This works fine with some basic controls but as soon as I have a datagrid inside the XAML, non of this works:

System.Windows.Markup.XamlWriter.Save throws:

Cannot serialize a generic type 'System.Collections.ObjectModel.ObservableCollection`1[Microsoft.Windows.Controls.DataGridColumn]'.

System.Xaml.XamlServices.Save throws:

Constructor on type 'System.ComponentModel.ComponentConverter' not found.

I already found in forum that XAML does not fully support generics, however, is there a solution for this? I'm a bit confused because if XAML can be read dynamically it should also be possible to save it...

I use .NET Framework 4.

Thanks for reply... any suggestion is very welcome.

View Complete Post

More Related Resource Links

Serialization and generics : problem with xmlinclude

Hello everyone. I've got a hierarchy of class like this: [Serializable] [xmlinclude(DiscreteDomain)] public abstract class Domain { } [Serializable] [xmlinclude(DiscreteDomain<>)] public abstract class DiscreteDomain : Domain { } [Serializable] public sealed class DiscreteDomain<T> : DiscreteDomain { } I can't serialize an object of type DiscreteDomain<T> because xmlinclude can't be used with a generics like DiscreteDomain<> (a type must be specified but i can't).   How can I serialize this type of object ?   Thx, JP

XAML Serialization bug

Hi all, I have this class public class XamlBugClass { List<string> myList = new List<string>(); public List<string> MyList { get { return myList; } set { myList = value; } } } this XAML <XamlBugClass xmlns="clr-namespace:WindowsFormsApplication1;assembly=WindowsFormsApplication1" > <XamlBugClass.MyList> <String xmlns="clr-namespace:System;assembly=mscorlib" >hello world</String> <String xmlns="clr-namespace:System;assembly=mscorlib" >hello world2</String> </XamlBugClass.MyList> </XamlBugClass> and I do this operation object t = XamlReader.Load(File.OpenRead("MySample.xaml")); string XAML = XamlWriter.Save(t); The Load just works perfect but when I get to the Save I get "Cannot serialize a generic type 'System.Collections.Generic.List`1[System.String]'."   This does not make sense at all, so I consider it a bug. Cheers, Tobias

XAML Serialization replacement

I've got an app where the user lays out a windows with various information displays.  To save the layout the window is serialized using XAML Serialization vial XAMLWriter.Save. the problem is all of the controls are user controls and as XAML Writer is"self contained" it serializes all the contents of the usercontrol as well.  You end up with the control, its resources, contextmenus and component controls being serialized. Does any one know of a solution to this problem so only the XAML for the user control only? I'd role my own version of this, but don't really know the best way to do so.   any help would be appreciated.  

Problem with serialization of objects

When I try to return a collection (an List<>) of a complex kind of object (each one have about 20-30 custom objects inside) I get this error:

Test method SMILTest.ServiceManagerTest.PessoasTest threw exception:  System.ServiceModel.CommunicationObjectFaultedException: The communication object, System.ServiceModel.Security.SecuritySessionClientSettings`1+ClientSecurityDuplexSessionChannel[System.ServiceModel.Channels.IDuplexSessionChannel], cannot be used for communication because it is in the Faulted state..

Server stack trace:
   at System.ServiceModel.Channels.CommunicationObject.ThrowIfFaulted()
   at System.ServiceModel.Security.SecuritySessionClientSettings`1.ClientSecurityDuplexSessionChannel.TryReceive(TimeSpan timeout, Message& message)
   at System.ServiceModel.Dispatcher.DuplexChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Obje

problem parse xaml and blank spaces


Hi, I have a problem parsing an xaml with characters &#xD; and space blanks, the space that show me is different that show in Silverlight.

this is the code to parse the xaml

            xml_node = nodo.OuterXml;
            text = (System.Windows.Controls.TextBlock)XamlReader.Parse(xml_node);

            if (text.Height != double.NaN)
              text.Height = double.NaN;

            text.Text = text.Text.Replace(' ', (char)160);

            if (text.Visibility != Visibility.Collapsed)

And this is the code of xaml:

  <TextBlock x:Name="Elemento_300920102232365" Canvas.Left="121.488" Canvas.Top="125.682" Width="1705.799" Height="2169.455" Text="&#xD;&#xD;&#xD;Jamón de Bellota (reserva) / Pernil de Bellota (reserva)                         17'00 €&#xD;Anchoas del Cantábrico / Anchoves del Cantàbric  &

Attached Event Registering Problem in XAML vs. CS



I spent hours searching for a solution to this, but did not find a proper one.

As stated in this post here, there seems to be a difference in how attached events are registered in XAML and in C#.

As described in the MSDN documentation , WPF requires you to implement two methods (AddXYZHandler, RemoveXYZHandler) in order to use an attached Event (XYZEvent) in XAML:


public static readonly RoutedEvent XYZEvent = EventManager.RegisterRoutedEvent("XYZ", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(XYZClass));

public static void AddXYZHandler(DependencyObject d, RoutedEventHandler handler)
 //event registration comes here

public static void RemoveXY

Serialization Problem....



i am trying this code but its giving me "There is an error in XML document (0, 0)."

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.IO;

namespace GridViewPaging
    public class Person
        private string _name;
        private CNIC _cnic;

        public string Name
                _name = value;
                return _name;

        public string NIC
                _cnic.NIC = value;
                return _cnic.NIC;


    public struct CNIC
        private string _cnic;
        private bool _isValid;

        public string NIC
                if (IsCNICValid(value))
                    _isValid = true;
                    _cnic = value;
                    _isValid = false;
                if (

Problem streaming xaml file


So I am trying to stream the file "test.xaml" (on the root of my project) to a canvas. I have tried numerous methods and for some reason when I call it to stream the xaml document but it doesn't.

My Code:

System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("test.xaml");
StreamReader sr1 = new StreamReader(s); 
string xamlString = sr1.ReadToEnd(); 

canvas1 = XamlReader.Load(new System.IO.StreamReader(s).ReadToEnd().ToString()) as Canvas;


To debug, I made a textblock element called debug1 and I wanted to send my code stream as a string to the textblock (to see where my problem is).

Whats interesting is that after line 2 no code will be executed; I know this because the debug1 textblock isnt populated with any data (it remains with its default "textblock" text value). I do not get any errors back or anything.

I know how to put prewritten xaml on a canvas in a ton of ways. I just want to know why xaml file is not streaming at all (is the code right?) (is there something im forgetting?) (is there a better way of going about this?)

Quicknote: My xaml file is just a page xaml document, there's nothing special about it format wise. Also I have been searching a day now and I haven't found anyone with the same issue.

cider xaml serialization


I have a property that is of an abstract base class type.  This class has a typeconverter the can covert to and from string so that I can write xaml like so

<MyControl Prop="Value1" />

However, when I edit this in the property editor I get this xaml instead



  <ns:Value1 />



This doesn't work in my scenario because "Value1" in this case is a private inner class.  I have to have Cider serialize it as a string.  Is there any way to force it to do so?

I can provide an example if necessary.

A good example if just going through this walkthough of creating a type converter

Activity field xml serialization problem


Hello,first of all I beg your pardon if my question could be out of topic, but I don't know where to address it. I'm working with 3.5 workflow foundation and I have a custom activity with a field of type DictionaryList, which is a serializable class containing some simple typed members, and a FeedbackItem[] member. FeedbackItem is a serializable class containing an ID (int), a Type (System.Type) and a Value (System.Object) fields. In the designer when I edit the custom activity, and I set the List Property (by using a PropertyGrid) i fill the DictionaryList class with all fields including the array of FeedbackItem. When I save the designer saves the workflow in a xoml file. When I load the xoml in the designer and I open the designer of the List property of the activity the DictionaryList object stored in previous session is missing the contrent of the FeedbackItem array; the FeedbackItems property has a FeedbackItem[0]. It seems like the xml serializer serializes the DictionaryList object but it can't deserialize it completely. Is there something I have to beware of? It's possible that some field in the FeedbackItem is not serializable and causes some error in deserialization process? I don't understand because il I edit the xoml file I can read the array like this:

    <x:Array Type="{x:Ty

Problem when doing a binary serialization from COM



I am trying to serialize an object using binary and I'm running into an error "Unable to load assembly". I tried creating a new SerializationBinder which loads the correct assembly however it returns an error "The object with ID 7 was referenced in a fixup but does not exist" (I also received the same error but with ID 6).

My code runs in the following way:

A COM visible dll is launched from a VB6 application. This DLL has a reference to a .NET dll which contains the data objects and performs the serialization.

However, If add a new Windows Forms application to the solution, and call the same methods using the same data objects deserialization works without any issues, and no need to reset the binding.

Thank you

Problem to group radio button across gridview rows

In general no one can easily group or make a single selection from radio button list inside gridview rows. There are a lot of way to make a single or unique selection from list. Here i want to share how i can address this problem in the most easiest way. Googling the problem most of the cases i saw the grouping in horizontal way. Thats why i tried to group the radio buttons in vertical manner. Let i have a requirement like below:

Regrading Sharpoint 2010 problem and solutions

We can write your question and problem on this blogger.you get ans.use.

This sharpoint 2010 problem solving threads

Hi friend,

you can use this thread to solving problem and solutions.

What's new in XAML of .NET 4.0

With .NET 4.0 Microsoft will bring up a improved version of XAML. This arcitle shows you the language enhancements they made.
Easy Object References with {x:Reference}

Built-in Types

Generics in XAML with x:TypeArguments

Support for Arbitrary Dictionary Keys

Use of Non-Default Constructors with x:Arguments

Use of Static Factory Methods with x:FactoryMethod

Virtual Path Problem


 I get the following error on last line below (working on remote host): 'E:\web\sfcascom\ls_admin/uploads/images/001.JPG' is not a valid virtual path,

Note that the slashes go in different directions

I've tried a number of things but nothing seems to work. Thanks-


Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Web.UI.WebControls

Imports System.Drawing.Imaging.ImageCodecInfo
Partial Class Protected_imageupload
    Inherits System.Web.UI.Page
    Private Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        ' Initialize variables
        Dim sSavePath As String
        Dim sThumbExtension As String
        Dim intThumbWidth As Integer
        Dim intThumbHeight As Integer
        Dim sFilename As String = ""
        ' Set constant values
        'sSavePath = "images/"
        sSavePath = Server.MapPath("~/") + "ls_admin/uploads/images/"

        sThumbExtension = "_thumb"
        intThumbWidth = 160
        intThumbHeight = 120

        ' If file field isn?t empty
        If FilUpload.PostedFile IsNot Nothing Then
            ' Check file size (mustn?t be 0)
            Dim myFile As Http

Strange Photo upload problem


I'm working project it's live on shared hosting

we'er uploading photos to a file on the server everything works fine the photo is resized renamed and sent to the folder

the upload page returns the thumbnail and the full size photo it's there all the photos have to be approved by an admin

twice now after they have been approved they disapear I checked the folder they are not there could there be anywhere

on the server they could get hung up before they go into the folder I have it setup so when you delete the data base entry it deletes the photo from the folder othe than that you need to open the folder and delete the photo this has hapened twice now I tried to recreate it with no luck

thaks for your help in advance

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend