How to serialize a derived class in Silverlight


I created a custom control in XAML, and added some custom properties as well. Now, I want to serialize it to JSON if possible. Here is (essentially) what I have:

public partial class MyCustomClass : UserControl
    public Dictionary<char, int[]> ValueMap;
    public int Value { get; set; }

And in the code that handles serialization:

public static string Serialize(object objectToSerialize)
    using (MemoryStream ms = new MemoryStream())
        DataContractJsonSerializer serializer = 
          new DataContractJsonSerializer(objectToSerialize.GetType());
        serializer.WriteObject(ms, objectToSerialize);
        ms.Position = 0;
        using (StreamReader reader = new StreamReader(ms)) 
          return reader.ReadToEnd();

However, serializer.WriteObject(ms, objectToSerialize); throws


Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required."

Now, when I do add those attributes to the MyCustomClass, I of course get the same exception, only this time for System.Windows.UIElement instead of MyCustomClass.

So, is there a way to serialize my custom derived class with the existing serialization method, or should I just write a custom serialization methods for MyCustomClass?


I think you are better off implementing IXmlSerializable here, as you really don't want to indiscriminately serialize everything in the base class (and I don't believe you can, quite frankly).

Rather, implement IXmlSerializable on MyCustomClass, and then the DataContractJsonSerializer will be able to use that implementation to serialize to/from JSON.

By : casperOne

I have a service with almost the exact signature. I can pass values that have a "." in the name. For example, this would work on mine:

[WebGet(UriTemplate = "category/{id}")]
string category(string id);

with the url http://localhost/MyService.svc/category/test.category I get the value `"test.category" passed in as the string value.

So there must be some other issue. how are you accessing the URL? just directly in the browser? Or via a javascript call? Just wondering if it is some error on the client side. The server passes the value just fine. I would recommending trying to access the url in your browser, and if it doesn't work then post exactly what URL you are using and what the error message was.

Also, are you using WCF 3.5 SP1 or just WCF 3.5? In the RESTFul .Net book I'm reading, I see there were some changes with regards to the UriTemplate.

And finally, I modified a simple Service from the RESTFul .Net book that works and I get the correct response.

class Program
    static void Main(string[] args)
        var binding = new WebHttpBinding();
        var sh = new WebServiceHost(typeof(TestService));
        Console.WriteLine("Simple HTTP Service Listening");
        Console.WriteLine("Press enter to stop service");

public class TestService
    [WebGet(UriTemplate = "category/{id}")]
    public string category(string id)
        return "got '" + id + "'";

That is true that a path part cannot contain a period or many other special characters for that matter. I experienced the same problem a while back and received an answer from TechNet team stating that querystring is your only option the could find. Sorry

By : bendewey

This video can help you solving your question :)
By: admin