none
XML and JSON Serialization Issues RRS feed

  • Question


  • Here are the details of the problem:

    I have a Property on an object that looks something like this:


    Public class MyObject

    {

            public Distance Width {get;set;}

    }

     

    If I implement ISerializable on the Distance object, it works just fine with the DataContract Serializer if the incoming data is in JSON format but fails with the following message when presented with XML:

    System.Runtime.Serialization.SerializationException: Element Unit from namespace  cannot have child contents to be deserialized as an object. Please use XmlNode[] to deserialize this pattern of XML. ---> System.Xml.XmlException: End element 'Unit' from namespace '' expected. Found text 'IN'. Line 1, position 116.

    The incoming XML node for Width looks like this (NOTE THERE IS NO NAMESPACE SPECIFIED ON THE INCOMING DATA OBJECT – THAT’S WHAT I WANT):


    <Width>

            <Unit>IN</Unit>

            <Value>20</Value>

    </Width>

     

    The ISerializable interface appears to force me to use a namespace because it serializes outbound XML like this:

     
    <Width xmlns:d4p1="http://schemas.datacontract.org/2004/07/Harmony.DL.Library.Architecture">
            <Value xmlns:d5p1="http://www.w3.org/2001/XMLSchema" i:type="d5p1:double">20</Value>
            <Unit xmlns:d5p1="http://www.w3.org/2001/XMLSchema" i:type="d5p1:string">IN</Unit>
    </Width >

     

    Even if I tag the distance object with [XmlRoot(Namespace="")] attribute it still forces a namespace on the outbound object, leading me to believe this missing namespace is what is causing the serialization to fail when the inbound XML does not specify namespace.

     

    If I use a [DataContract(Namespace="")] Attribute with the appropriate data members instead of ISerializable, then everything works as expected in both JSON and XML. The problem is that this particular object (Distance) is not serialized exclusively using the data contract serializer, and I don’t want to mix Data Contract attributes with XML Serialization attributes – so how do I get the ISerializable implementation to work in the default DataContract Serializer for both JSON and XML when the inbound XML does not have a Namespace specified?

     

    Thanks,

    Mike Mlachak

    Monday, December 16, 2013 5:38 PM

All replies

  • Hi,

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 17, 2013 6:47 AM
    Moderator
  • By default, whenever we use Iserializable it uses DataContractSerializer.

    Further if we go by Serialization guidelines http://msdn.microsoft.com/en-us/library/6exf3h2k(v=vs.110).aspx  as presented here it looks like DataContractSerializer has full control over if instances of your type might need to be persisted or used in Web Services.

    However when we speak about XmlFormat it looks like we can gain full control only if we use xmlSerializer instead of DatacontractSerializer.

    So i think in your case JSON is working since datacontractserializer has full control over JSON.
    However for xml if we need to get full control we should be using xmlserializer.

    Thursday, January 23, 2014 8:53 PM