locked
Serializing large objects in .NET RRS feed

  • Question

  • Hi

    I have some code which essentially converts from one object type to another by serializing the object and writing to an XmlDocument and saving, then loading the file and deserialising to the new object type. The types are actually the same structure however are from difference WCF services. This works fine until I encountered a very large object and the xmlSerializer throws an out of memory exception. Is there any other way I can convert between these 2 types?

    public static bool ConvertMe(Service1.MyObj myobj1, ref Service2.MyObj myobj2)
    {
      XmlSerializer s = new XmlSerializer(typeof(Service1.MyObj));
      StringWriter sw = new StringWriter();
      XmlTextWriter xw = new XmlWriter(sw);
    
      s.Serialize(xw, myobj1);
      XmlDocument doc = new XmlDocument ();
      doc.LoadXml(sw.ToString());
      doc.Save("c:\\test.xml");
    
      TextReader tr = new StreamReader("c:\\test.xml");
      XmlSerializer s2 = new XmlSerializer(typeof(Service2.MyObj));
      myobj2 = (Service2.MyObj)s2.Deserialize(tr);
      tr.Close();
    
    }
    
    
    

    Many Thanks

    Nicki

     

    Wednesday, November 24, 2010 5:16 PM

All replies

  • this is difficult to answer without being aware of your context.

    If i were you i would create a system.componentmodel.typeconverter though...

    Look into it.

    Friday, November 26, 2010 1:59 PM
  • I actually managed to solve it by directly creating the new object and using AutoMapper to map the properties

    Thanks though

    Thursday, December 30, 2010 4:16 PM
  • If you are the owner of both services and the client app that is calling them you could make use of the /r option in svcutil.  Put the common data structures in a seperate assembly and specify the assembly in svcutil command line when generating the proxy.  It only works with data contracts and the client would have to have the common assembly.
    Saturday, January 15, 2011 4:21 AM