Question

I'm trying to serialize an XML file between two software components, with a client-server fashioned code.

First of all, i'll show how i solved the same issue for Bitmaps (code has been simplified for writting this):

Initially, this code serializes me Bitmaps at the server side:

    public String ImageToString(Bitmap img)
    {
        byte[] byteArray = new byte[0];
        using (MemoryStream stream = new MemoryStream())
        {
            img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            stream.Close();
            byteArray = stream.ToArray();
        }
        return Convert.ToBase64String(byteArray);
    }

And this deserializes the Bitmap at the client side:

    public Bitmap StringToImage(string imageString)
    {
        byte[] array = Convert.FromBase64String(imageString);
        Bitmap image = (Bitmap)Bitmap.FromStream(new MemoryStream(array));
        return image;
    }

What i really want to do now is do the same, but with XML files. I have at the server side:

    public XmlDocument RemoteXMLfile()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("myXMLFile.xml");

        return doc;
    }

But this makes the compiler to complain because it finds the XML document unmarked as serializable.

My question is: how do i solve this? Thanks!

No correct solution

OTHER TIPS

I answer my own question:

The XMLDocuments is loaded and converted to a byte array at the server side:

public byte[] RemoteXMLfile()
{
    XmlDocument doc = new XmlDocument();
    doc.Load("myLocalXMLfile.xml");

    byte[] byteArray = new byte[0];
    using (MemoryStream stream = new MemoryStream())
    {
        doc.Save(stream);
        stream.Close();
        byteArray = stream.ToArray();
    }
    return byteArray;
}

And at the client side, we convert again to an XML document from its byte array shape:

    byte[] docbytearray = RemoteXMLfile();
    XmlDocument doc = new XmlDocument();
    MemoryStream ms = new MemoryStream(docbytearray);
    doc.Load(ms);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top