将 ASMX Web 服务的响应读回磁盘中的对象

发布于 2024-08-26 07:12:24 字数 187 浏览 8 评论 0原文

我们有一个 ASP.Net Web 服务,它将 XML 文件接收到 PDA 并将其映射到对象(标准 .Net Web 方法)。是否有一种简单的方法可以在将此 XML 响应保存到磁盘后,将其读回到之前的相同对象结构中?

PDA 应用程序在接收到从 Web 服务返回的数据后会自动执行此操作,但找不到一种方法可以让我提供流等以“离线”重复该过程。

We have an ASP.Net web service which receives an XML file to a PDA and maps it to objects (a standard .Net web method). Is there an easy method of after saving this XML response to disk, having it read back into the same object structure it was in before?

The PDA application automatically does this after receiving the data back from the web service but couldn't find a method that would let me supply a stream or the like to repeat the process 'offline'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

这个俗人 2024-09-02 07:12:24

序列化允许您执行此操作 - 这实际上是通过 Web 服务发送对象的方式。

如果你幸运的话,下面的代码将序列化一个对象(称为“object_type”类型的“object”。)

XmlSerializer serialiser = new XmlSerializer(typeof(object_type));
FileStream stream = new FileStream(@"C:\Temp\serialised_file.xml", FileMode.Create);
serialiser.Serialize(object, stream);

并反序列化:

XmlSerializer serialiser = new XmlSerializer(typeof(object_type));
FileStream stream = new FileStream(@"C:\Temp\serialised_file.xml", FileMode.Open);
object_type object = serialiser.Deserialize(stream) as object_type;

我说“如果你幸运的话”,因为 90% 的时间对我有用。如果类中的属性是抽象类,则可能需要在 XmlSerializer 构造函数中声明扩展该抽象类的所有类类型。还要注意类内没有“循环依赖”。

Serialization allows you to do this - it's actually how objects are sent across a web-service.

If you're lucky, the following code will serialise an object (called "object" of type "object_type".)

XmlSerializer serialiser = new XmlSerializer(typeof(object_type));
FileStream stream = new FileStream(@"C:\Temp\serialised_file.xml", FileMode.Create);
serialiser.Serialize(object, stream);

And to de-serialise:

XmlSerializer serialiser = new XmlSerializer(typeof(object_type));
FileStream stream = new FileStream(@"C:\Temp\serialised_file.xml", FileMode.Open);
object_type object = serialiser.Deserialize(stream) as object_type;

I say "if you're lucky" because 90% of the time that works for me. If you have properties within the class that are abstract classes you may need to declare all class types that extend that abstract class in the XmlSerializer constructor. Also be careful there are no "circular dependencies" within the class.

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