如何将 XmlSerializer 类发送到 WebService 然后反序列化它?
我希望能够通过 WebService 发送 XmlSerializer 类(在远程 C# 应用程序中明显生成),然后将其反序列化为类。 (我也不知道它可能)
我的类是:
SystemInfo
我以这种方式序列化它:
XmlSerializer mySerializer = new XmlSerializer(typeof(SystemInfo));
StreamWriter myWriter = new StreamWriter(textBox1.Text);
mySerializer.Serialize(myWriter, sysinfo);
如何构建 WebService?
[WebMethod]
public void Reports(XmlSerializer xmlSerializer)
{
.
.
.
}
有人可以帮我吗?
问候
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我假设您想要将任意类型传递给单个 Web 方法,其中类型由客户端和服务器共享。
发送 XmlSerializer 没有太多意义,它只有序列化/反序列化的逻辑。它没有实际数据,即读/写到流中的数据。您应该做的是传递一个字符串或 XmlNode。
然后,Web 服务的调用者可以使用 XmlSerializer 的客户端实例并将对象序列化为字符串,然后调用将字符串作为参数传递的 Web 方法。然后,Web 方法本身可以创建 XmlSerializer 的实例并将字符串反序列化回对象。当然,要创建序列化程序的服务器大小实例,您需要知道为其创建序列化程序的根类型,您可以将其作为类型名称传递并使用 Type.GetType() 获取要传递给 XmlSerializer 的正确类型。
如果您预先知道要传递哪些类型,那么您还可以声明您的 Web 方法更强类型,并为您期望接收的类型显式创建方法。
如果有线格式不太重要,您还可以使用
SoapFormatter
或BinaryFormatter
处理序列化/反序列化。在 BinaryFormatter 的后一种情况下,您将声明您的 Web 方法以采用byte[]
参数,这些格式化程序(序列化程序)的优点是,它们在创建时不需要有关类型的附加信息格式化程序的实例,但它们可能比 XmlSerializer 慢更新:添加了一些简单的示例(未经测试)
使用 XmlSerializer 的示例,这里您需要从客户端传递类型名称,因此我把它作为一个额外的论点。
使用 BinaryFormatter 的示例,不需要类型名称,但类类型需要为 可序列化
在客户端,您可以使用类似以下内容的 BinaryFormatter 来进行序列化。
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 aBinaryFormatter
to handle the serialization/deserialization. In the later case of the BinaryFormatter you would declare your web method to take abyte[]
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 XmlSerializerUpdate: 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.
Example using a BinaryFormatter, no type name needed but you the class types will need to be serializable
On the client side you would use something like the following to serialize using the BinaryFormatter.
克里斯,谢谢你帮助我。这是向前迈出的重要一步。
我解决了发送 xml 字符串的问题:
和 WebService:
现在它的工作就像一个魅力:)
我的问题是: 我可以改进代码吗?
谢谢
Chris, thanks for helping me out. It was a major step forward.
I solved the problem sending the xml string:
And the WebService:
Its working like a charm now :)
My question is: Can i improve the code?
Thanks