views:

199

answers:

2

In our app, we need to save properties of objects to the same database table regardless of the type of object, in the form of propertyName, propertyValue, propertyType. We decided to use XamlWriter to save all of the given object's properties. We then use XamlReader to load up the XAML that was created, and turn it back into the value for the property. This works fine for the most part, except for empty strings. The XamlWriter will save an empty string as below.

<String xmlns="clr-namespace:System;assembly=mscorlib" xml:space="preserve" /> 

The XamlReader sees this string and tries to create a string, but can't find an empty constructor in the String object to use, so it throws a ParserException.

The only workaround that I can think of is to not actually save the property if it is an empty string. Then, as I load up the properties, I can check for which ones did not exist, which means they would have been empty strings.

Is there some workaround for this, or is there even a better way of doing this?

A: 

We had a similar problem to this when trying to serialize strings. The only way we could resolve it was to create a StringWrapper struct or class that had the appropriate constructors. We then used this type to load and save our string values.

Jeff Yates
A: 

I am also facing the same issue while saving and loading the xaml. I can't apply above two solutions as everythign is happening at runtime.

I came across one more solution but that too is design time solution -

http://krishnabhargav.blogspot.com/2009/06/declaring-empty-string-in-xaml.html

Anybody knows any other solution for this problem :(

akjoshi
We originally tried using serialization, then we had turned to XAML reader/writer. Eventually we went back to using serialization. MSDN forum members said XML serialization is the correct approach.http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7c26e47e-b783-4972-97cf-e3ac814b892c/#3ea46eae-cc63-4ee1-ab26-53437f033f01
sub-jp