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

发布于 2024-09-03 14:07:44 字数 558 浏览 8 评论 0原文

我希望能够通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还给你自由 2024-09-10 14:07:44

首先,我假设您想要将任意类型传递给单个 Web 方法,其中类型由客户端和服务器共享。

发送 XmlSerializer 没有太多意义,它只有序列化/反序列化的逻辑。它没有实际数据,即读/写到流中的数据。您应该做的是传递一个字符串或 XmlNode。

然后,Web 服务的调用者可以使用 XmlSerializer 的客户端实例并将对象序列化为字符串,然后调用将字符串作为参数传递的 Web 方法。然后,Web 方法本身可以创建 XmlSerializer 的实例并将字符串反序列化回对象。当然,要创建序列化程序的服务器大小实例,您需要知道为其创建序列化程序的根类型,您可以将其作为类型名称传递并使用 Type.GetType() 获取要传递给 XmlSerializer 的正确类型。

如果您预先知道要传递哪些类型,那么您还可以声明您的 Web 方法更强类型,并为您期望接收的类型显式创建方法。

如果有线格式不太重要,您还可以使用 SoapFormatterBinaryFormatter 处理序列化/反序列化。在 BinaryFormatter 的后一种情况下,您将声明您的 Web 方法以采用 byte[] 参数,这些格式化程序(序列化程序)的优点是,它们在创建时不需要有关类型的附加信息格式化程序的实例,但它们可能比 XmlSerializer 慢

更新:添加了一些简单的示例(未经测试)

使用 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
}

使用 BinaryFormatter 的示例,不需要类型名称,但类类型需要为 可序列化

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

在客户端,您可以使用类似以下内容的 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());

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());
思慕 2024-09-10 14:07:44

克里斯,谢谢你帮助我。这是向前迈出的重要一步。

我解决了发送 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);

和 WebService:

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

现在它的工作就像一个魅力:)

我的问题是: 我可以改进代码吗?

谢谢

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文