We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Serializing Document in .Net


I am trying to Serialize a Document to be passed to a client over the internet but this line of code doesnt work

XmlSerializer xs = new XmlSerializer(typeof(Document));

The error I get when I run the code above is this.

Server was unable to process request. --> You must implement the Add(System.Object) method on Aspose.Words.Document because it inherits from IEnumerable.

How do I implement the add method on the document object.

Thanks George


I’m not sure it is possible. Serialization is the process of converting an object’s public properties and fields to a serial format, but what about its internal state? I think it’s a much better idea to save the document to a file and send it to the client who can then restore the Document object based on the file.

Regarding your question, here is the answer from MSDN:

The XmlSerializer gives special treatment to classes that implement IEnumerable or ICollection. A class that implements IEnumerable must implement a public Add method that takes a single parameter. The Add method’s parameter must be of the same type as is returned from the Current property on the value returned from GetEnumerator, or one of that type’s bases. A class that implements ICollection (such as CollectionBase) in addition to IEnumerable must have a public Item indexed property (indexer in C#) that takes an integer, and it must have a public Count property of type integer. The parameter to the Add method must be the same type as is returned from the Item property, or one of that type’s bases. For classes that implement ICollection, values to be serialized are retrieved from the indexed Item property, not by calling GetEnumerator.

Oh, and another technique: since MemoryStream is serializable, you can save the document to the stream and serialize it then.

Thanks for the idea. I will try it and let you know the results.

Many thanks for the prompt response.



Thanks for the idea. I have done it and it works!

I serialize the document as an XmlDocument to the client. The client then deserializes the XmlDocument to Aspose document. Great stuff.

This is the code to serialize the document to XmlDocument.

/// Function to convert a Document to XmlDocument
/// Document that is to be serialized to XML
/// XmlDocument 
public static XmlDocument SerializeToXmlDocument(Document doc)
    //Save the Aspose.Doc into a memory stream.
    MemoryStream stream = new MemoryStream();
    doc.Save(stream, SaveFormat.WordML);//SaveFormat.WordML);
                                        //Seek to the beginning so it can be read by XmlDocument.
    stream.Seek(0, SeekOrigin.Begin);

    //Load the document into an XmlDocument
    XmlDocument xmlDoc = new XmlDocument();


    return xmlDoc;


This is the code to DeSerialize the xml document created by the document

public static Document DeserializeXmlDocument(XmlDocument xmldoc)

    // Save the XML to a memory stream.
    MemoryStream memStream = new MemoryStream();
    ASCIIEncoding AE = new ASCIIEncoding();
    string xmlStr = xmldoc.DocumentElement.OuterXml;

    // // Write the xml to memory
    memStream.Write(AE.GetBytes(xmlStr), 0, xmlStr.Length);
    //Rewind stream just to make sure its at beginning.
    memStream.Seek(0, SeekOrigin.Begin);

    //Load the stream into a document.
    Document doc = new Document(memStream);

    //Close the stream
    return doc;


George France

Thank you George for sharing your code. It worked perfectly.

Thanks, Tyler