简体   繁体   English

如何将XmlSerializer类发送到WebService,然后反序列化?

[英]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. 我希望能够通过WebService发送XmlSerializer类(在远程C#应用程序中明显生成),然后将其反序列化为一个类。 (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? 如何构建WebService?

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

Can anyone help me out? 谁能帮我吗?

Regards 问候

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. 首先,我假设您想将任意类型传递给单个Web方法,该类型由客户端和服务器共享。

There is not much point in sending the XmlSerializer, it only has the logic to serialize/deserialize. 发送XmlSerializer并没有多大意义,它只具有序列化/反序列化的逻辑。 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. 您应该做的是传递一个字符串或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. 然后,Web服务的调用者可以XmlSerializer的客户端实例并将对象序列化为字符串,然后调用将字符串作为参数传递的Web方法。 The web method it self can then create an instance of a XmlSerializer and deserialize the string back into an object. 然后,它自己的Web方法可以创建XmlSerializer的实例,并将字符串反序列化为对象。 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. 当然,要创建序列化程序的服务器大小实例,您将需要知道为其创建序列化程序的根类型,可以将其作为类型名称传递,并使用Type.GetType()获取正确的类型以传递给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. 如果您预先知道要传递的类型,则还可以声明Web方法的类型更强,并为希望接收的类型显式创建方法。

If the wire format is not too much of a concern, you could also user SoapFormatter or a BinaryFormatter to handle the serialization/deserialization. 如果不需要太多的连线格式,则还可以使用SoapFormatterBinaryFormatter来处理序列化/反序列化。 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 在BinaryFormatter的后一种情况中,您将声明Web方法采用byte[]参数,这些格式化程序(序列化器)的优点是,在创建格式化程序的实例时,它们不需要有关类型的其他信息,但是它们可能比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. 使用XmlSerializer的示例,在这里您将需要从客户端传递类型名称,因此我将其作为附加参数。

[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 使用BinaryFormatter的示例,不需要类型名称,但您的类类型将需要可序列化

[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. 在客户端,您将使用类似于以下的内容通过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());

Chris, thanks for helping me out. 克里斯,谢谢你帮助我。 It was a major step forward. 这是向前迈出的重要一步。

I solved the problem sending the xml string: 我解决了发送xml字符串的问题:

        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: 和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 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM