pee2002
pee2002

Reputation: 177

How to send a XmlSerializer Class to a WebService and then Deserialize it?

I want to be able to send a XmlSerializer class (which is generated obvious in remote C# application) over a WebService that will then deserialize it into a class. (I didnt know it its possible either)

My class is:

SystemInfo

I'm serializing it this way:

        XmlSerializer mySerializer = new XmlSerializer(typeof(SystemInfo));
        StreamWriter myWriter = new StreamWriter(textBox1.Text);
        mySerializer.Serialize(myWriter, sysinfo);

How can i build the WebService?

    [WebMethod]
    public void Reports(XmlSerializer xmlSerializer)
    {
      .
      .
      .
    }

Can anyone help me out?

Regards

Upvotes: 2

Views: 3378

Answers (2)

pee2002
pee2002

Reputation: 177

Chris, thanks for helping me out. It was a major step forward.

I solved the problem sending the xml string:

        SystemInfo sysinfo = new SystemInfo();
        sysinfo.RUN();

        XmlSerializer mySerializer = new XmlSerializer(typeof(SystemInfo));

        MemoryStream memStream = new MemoryStream();
            mySerializer.Serialize(memStream, sysinfo);
            memStream.Seek(0, System.IO.SeekOrigin.Begin);
            XmlDocument doc = new XmlDocument();
            doc.Load(memStream);
        memStream.Close();

        localhost.WS_Agente dasdsa = new localhost.WS_Agente();
        dasdsa.Reports(doc.InnerXml);

And the WebService:

    [WebMethod]
    public void Reports(string xml)
    {
        XmlSerializer mySerializer = new XmlSerializer(typeof(SystemInfo));
        SystemInfo obj = (SystemInfo)mySerializer.Deserialize(new StringReader(xml));
    }

Its working like a charm now :)

My question is: Can i improve the code?

Thanks

Upvotes: 1

Chris Taylor
Chris Taylor

Reputation: 53709

First I assume you want to pass arbitrary types to a single web method, where the types are shared by the client and the server.

There is not much point in sending the XmlSerializer, it only has the logic to serialize/deserialize. It does not have the actual data, that is either read/written to a stream. What you should do is pass either a string or and XmlNode.

The caller of the web service can then a client side instance of XmlSerializer and serialize the object to a string, then call the web method passing the string as an argument. The web method it self can then create an instance of a XmlSerializer and deserialize the string back into an object. Of course to create the server size instance of the serializer you will need to know the root type to create the serializer for, you can pass this as a type name and use Type.GetType() to get the correct type to pass to the XmlSerializer.

If you know upfront which types you are going to be passing then you could also declare your web method more strongly typed and explicitly create methods for the types you expect to recieve.

If the wire format is not too much of a concern, you could also user SoapFormatter or a BinaryFormatter to handle the serialization/deserialization. In the later case of the BinaryFormatter you would declare your web method to take a byte[] argument, the advantage of these formatters (serializers) is that they do not need additional info on the type when you create the instance of the formatter, but they can be slower than an XmlSerializer

Update: Added some simple examples (Untested)

Example using an XmlSerializer, here you will need to pass the type name from the client side, so I made it an additional argument.

[WebMethod]
public void Reports(string xml, string typeName)
{
  XmlSerializer xs = new XmlSerializer(Type.GetType(typeName));
  object obj = xs.Deserialize(new StringReader(xml));
  // use the deserialize object
}

Example using a BinaryFormatter, no type name needed but you the class types will need to be serializable

[WebMethod]
public void Reports(byte[] data)
{
  BinaryFormatter bf = new BinaryFormatter();
  object obj = bf.Deserialize(new MemoryStream(data));
  // use the deserialized object
}

On the client side you would use something like the following to serialize using the BinaryFormatter.

  // initialize the SystemInfo instance that you want to pass to the server
  SystemInfo si = new SystemInfo() { SystemName = "My System" };

  // Serialize to a memory stream
  BinaryFormatter bf = new BinaryFormatter();
  MemoryStream ms = new MemoryStream();
  bf.Serialize(ms, si);

  // Call the service, passing the array from the memory stream
  ws.Reports(ms.ToArray());

Upvotes: 3

Related Questions