client application object will not serialize and write out to file. RRS feed

  • Question

  • I am fairly new to WCF and have a test service up and running.  From the client winform I can retrieve data and show it on the form.  I want to take an object and write it out to a file in xml format.  I can write the file to my local machine but it is empty.  I have verified that the client object contains data before writing it.

    I went back to MSDN and followed the example here:

    public class Book
        public String title;


    public void WriteXML()
        Book overview = new Book();
        overview.title = "Serialization Overview";
        System.Xml.Serialization.XmlSerializer writer =
            new System.Xml.Serialization.XmlSerializer(typeof(Book));

        System.IO.StreamWriter file = new System.IO.StreamWriter(
        writer.Serialize(file, overview);

    This works fine on the machine; however, when I substitute my object returned from a WCF service call it doesn't write anything.

    Any suggestions are greatly appreciated.


    Wednesday, December 4, 2013 8:48 PM


All replies

  • Hello WB,

    I don't understand "when I substituted my object returned from a WCF Service".  

    Does the writer.Serialize() step thrown an exception?  Is it because the XmlSerializer is not typed correctly (typeof(Book)) would need to be changed to typeof(theobjectfromservice)?

    You could accomplish this using a generic method.  Take a look at this implementation

    You can use this to cast most objects that are serializable without error.




    Thursday, December 5, 2013 9:45 PM
  • Yes, I put my object in the place of Book.  my understanding is by default the wcf object being returned is serializable so I don't understand why it doesn't work.

    no error is thrown and the file is created it is just blank.

    Friday, December 6, 2013 12:32 AM
  • Ok, not sure without a trace of the message and the contract definition.  I recommend trying it with the link I sent because it also caters for WCF contracts and some exceptions.  If it still doesn't work then I will need more info to be helpful.

    This might be something I am overlooking between  System.Runtime.Serialization.DataContractSerializer and XmlSerializer.

    Good luck.


    Friday, December 6, 2013 6:24 AM