views:

314

answers:

3

EDIT: The solution to my problem is to implement IXMLSerializer. Thanks everyone for the quick responses.

Hi everyone, I'm having this issue and I do not seem to a find a proper solution to it.

I have the following class

public class Child
     {
      private int _id;

      public int Id
      {
       get { return _id; }
       set { _id = value; }
      }

     }
     [Serializable]
     public class Test: ISerializable
     {
      private int _id;

      public int Id
      {
       get { return _id; }
       set { _id = value; }
      }

      private Child _child = new Child();

      public Child Child
      {
       get { return _child; }
       set { _child = value; }
      }

      public Test()
      {
      }
      protected Test(SerializationInfo info, StreamingContext context)
      {
       if (info.MemberCount > 1)
        Child.Id = info.GetInt32("ChildId");
      }


      #region ISerializable Members

      public void GetObjectData(SerializationInfo info, StreamingContext context)
      {
       info.AddValue("ChildId", Child.Id);
      }

      #endregion
     }

If I send the following XML to be deserialzed

<?xml version="1.0" encoding="utf-16"?><Test><Id>0</Id><ChildId>10</ChildId></Test>

using the following code to deserialize

serializer.Deserialize(new StringReader("<?xml version="1.0" encoding="utf-16"?><Test><Id>0</Id><ChildId>10</ChildId></Test>");

I supposed that would call my Serialization constructor, but the thing is that is never getting called.

I am using the above posted XML instead of this one

<?xml version="1.0" encoding="utf-16"?><Test><Id>0</Id><Child><Id>10</Id></Child></Test>

Because I'm getting the xml posted from another application, so I need to manually created child objects and assigned its value manually from within the constructor.

I'll really appreciate all the help and guidance you can provide me.

+7  A: 

To customise XML serialization I believe you need to use IXmlSerializable rather than ISerializable.

I think that requires a parameterless constructor, and you then need to implement ReadXml and WriteXml. In other words, it doesn't use the same constructor as binary serialization.

Jon Skeet
+3  A: 

What is the serializer variable you use? If it's an XmlSerializer, your serialization hooks won't be called because the XmlSerializer doesn't support them (see IXmlSerializable).

HTH, Kent

Kent Boogaart
I'm using an XmlSerializer, but I though the Serialization constructor would be called when deserializing.
Paleta
What Serializer should I use?
Paleta
+2  A: 

XmlSerializer does not use SerializableAttribute, ISerializable nor the constructor taking the SerializationInfo info and StreamingContext parameters. Those constructs are used when using SoapFormatter (obsolete) and BinaryFormatter for serializing.

        BinaryFormatter bf = new BinaryFormatter();
        using (MemoryStream ms = new MemoryStream())
        {
            Class1 c1 = new Class1();
            bf.Serialize(ms, c1);

            ms.Position = 0;
            Class1 c2 = bf.Deserialize(ms) as Class1;
        }
Jakob Christensen
I'm trying to use a Binary Formatter along with a MemoryStream and write the XML inside the MemoryStream but I keep getting this error:"System.ArgumentException: Stream was not readable."I'm really lost here, the stream should be available for reading withing the BinaryFormatting
Paleta
I added some code to my answer. It works for me.
Jakob Christensen
Ok, but in the case you posted, I'm receiving an XML and want to deserialize it into a Class.How am I suppose to do that using BinaryFormatter?
Paleta
Sorry, I missed the point that you are receiving XML from another application. You are right that BinaryFormatter can not be used for this. In that case you will have to use XmlSerializer and IXmlSerializer as others have already pointed out.
Jakob Christensen