简体   繁体   English

什么时候应该使用XML序列化?

[英]When should XML serialization be used?

I've been doing some reading up on XML serialization, and from what I understand, It is a way to take an object and persist the state in a file. 我一直在阅读有关XML序列化的内容,根据我的理解,这是一种获取对象并将状态保存在文件中的方法。 As for the implementation, it looks straight forward enough, and there seems to be a load of resources for applying it. 至于实现,它看起来很直接,并且似乎有大量资源用于应用它。 When should XML serialization be used? 什么时候应该使用XML序列化? What are the benefits? 有什么好处? What are situations that are best helped by using this? 使用此功能最有帮助的情况是什么?

The .NET XmlSerializer class isn't the only way to persist an object to XML. .NET XmlSerializer类不是将对象持久化为XML的唯一方法。 The newer DataContractSerializer is faster, and also allows an object to be persisted to a binary form of XML, which is more compact. 较新的DataContractSerializer更快,并且还允许将对象持久化为XML的二进制形式,这更加紧凑。

The XmlSerializer is only getting limited bug fixes these days, in part because so much code depends on the precise details of how it works, in part because it is associated with ASMX web services, which Microsoft considers to be a "legacy technology". XmlSerializer目前只得到有限的错误修复,部分原因是因为它与工作原理的确切细节有很大关系,部分原因是它与ASMX Web服务相关联,微软认为这是一种“遗留技术”。

This is not the case with the DataContractSerializer , which continues to be a vibrant and important part of WCF. DataContractSerializer不是这种情况,它继续是WCF充满活力和重要的一部分。

You've answered a little bit of the question in your post. 你在帖子中回答了一些问题。 It's great for persisting the state of an object. 这对于持久化对象的状态非常有用。 I've used it in applications for saving user settings. 我在用于保存用户设置的应用程序中使用它。 It's also a great way to send data to other systems, since it is standardized. 这也是向其他系统发送数据的好方法,因为它是标准化的。 An important thing to remember is that it is easily human readable. 要记住的一件重要事情是它易于人类阅读。 This can either be a good or bad thing depending on your situation. 根据您的情况,这可能是好事也可能是坏事。 You might want to consider encrypting it, or using encrypted binary serialization if you don't want someone else to be able to understand it. 如果您不希望其他人能够理解它,您可能需要考虑加密它,或使用加密的二进制序列化。

EDIT: 编辑:
Another gotchya worth mentioning is that the .NET implemented XMLSerializer only serializes public members in a object. 另一个值得一提的是,.NET实现的XMLSerializer只序列化对象中的公共成员。 If you need to persist private or protected members, you will either need to use a customized serializer or use another form of serialization. 如果您需要保留私有或受保护的成员,则需要使用自定义序列化程序或使用其他形式的序列化。

Its good for communication between disparate systems. 它有利于不同系统之间的通信。 EG take a Java app and a C# app and allow them to communicate via a webservice with serializeable XML objects. EG使用Java应用程序和C#应用程序,并允许它们通过带有可序列化XML对象的Web服务进行通信。 Both apps understand XML and are shielded from the details of the other language. 这两个应用程序都理解XML,并且不受其他语言细节的影响。 And yes while you could fire strings back and forth, XML gives us strong typing and schema validation. 是的,虽然你可以来回发送字符串,但XML为我们提供了强大的输入和模式验证。

this is just from personal experiences - XML serialization is good for web services. 这只是来自个人经验 - XML序列化对Web服务很有用。 Also, if you want to modify (or allow the modification of) the object/file that you're storing to without using the application that you're writing (ie third party app), XML can be a good choice. 此外,如果您想要修改(或允许修改)您要存储的对象/文件而不使用您正在编写的应用程序(即第三方应用程序),XML可能是一个不错的选择。

I send an array of Objects of type class I wrote, using HttpWebRequest, so I cant send it as an object , because im mixing HttpWebRequest + Soap (that Im writing), and in Soap you cant send a non predefined Objects as String, int , ... . 我使用HttpWebRequest发送了一个类I类型的对象数组,因此我无法将其作为对象发送,因为我混合了HttpWebRequest + Soap(我正在编写),而在Soap中你不能发送非预定义的对象作为String,int ,......

so I used XML serialization to convert my object to an XML string and send it through my HttpWebRequest . 所以我使用XML序列化将我的对象转换为XML字符串并通过我的HttpWebRequest发送它。

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

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