views:

490

answers:

1

I have an Application (an appointment manager) which lets the user create usercontrols in a stackpanel per button click and the user can enter data into the usercontrols. So far so good.

I serialized the stackpanel with XamlWriter.Save(). But then when I try to load it again the next time the application starts with XamlReader.Load() I get the following exception:

"System.Windows.Markup.XamlParseException: Cannot set Name attribute value 'border1' on element 'Border'. 'Border' is under the scope of element 'Item', which already had a name registered when it was defined in another scope. Line '4' Position '43'."

I found a workaround in google involving removal of all control names which seem not to be useful for me.

Now you could ask, why do you have to use Xml-Serialisation at all. The thing is, that we have to create a science projekt for our xml-course, in which at least xml-serialisation and xslt must be used, so I thought this would be a great idea :(

+2  A: 

One piece of advice I would give you not to serialize the UI, but rather, serialize the underlying data.

For example, create a class called Data that represents the data stored in your user control. Make sure this class is decorated with the [Serializable] attribute. Use wpf's data binding capabilities to manage the data object CRUD operations. At this point, you can serialize and deserialize your data object using the XmlSerializer. When deserializing, bind your user control to the data object and you are done.

Wish I had time to write a quick sample.

Hope this helps

siz