简体   繁体   English

XML序列化与LINQ到XML

[英]XML Serialization vs LINQ to XML

I have few questions on XML serialization and LINQ to XML. 我对XML序列化和LINQ to XML几乎没有任何疑问。

When do we use XML Serialization and LINQ to XML? 我们什么时候使用XML Serialization和LINQ to XML? Please specify the kind of situations or scenarios to use XML Serialziation and LINQ to XML. 请指定使用XML Serialziation和LINQ to XML的情况或场景的类型。

What are the differences between XML Serialization and LINQ to XML? XML Serialization和LINQ to XML之间有什么区别?

What are the benefits of using XML Serialziation over LINQ to XML and vice-versa. 使用XML Serialziation而不是LINQ to XML有什么好处,反之亦然。

I have read in msdn and various blogs about DOM vs LINQ to XML but could not find much information on XML Serialization vs LINQ to XML. 我在msdn和各种关于DOM和LINQ to XML的博客中读过但是找不到有关XML Serialization vs LINQ to XML的更多信息。 It would be great if we can consolidate all the methods of XML in .NET with pros and cons. 如果我们能够利用优点和缺点来整合.NET中的所有XML方法,那就太棒了。

Thanks for your help in advance. 感谢您的帮助。

Regards, 问候,

Sushma Sushma

They are quite different things: 他们是完全不同的东西:

  • XML serialization allows you get the XML representation of a whole object, and to convert it back to an object. XML序列化允许您获取整个对象的XML表示形式,并将其转换回对象。 It is used when you need to store the current state of an object or to transfer it to a remote location. 当您需要存储对象的当前状态或将其传输到远程位置时,可以使用它。

  • LINQ to XML (or the XML classes of the .NET framework) is used to generate and process XML files that are obtained "as-is" and are not directly mapped to objects, such as data from RSS feeds. LINQ to XML(或.NET框架的XML类)用于生成和处理“按原样”获取的XML文件,而不是直接映射到对象,例如来自RSS提要的数据。

I totally agree with Konamiman answer. 我完全同意Konamiman的回答。 However you shouldn't also forget the fact that during the traversal, navigation if you will, process we usually deserialize the XML. 但是,您不应该忘记在遍历期间,如果您愿意导航,我们通常会对XML进行反序列化。 This make the difference a bit subtle. 这使得差异有点微妙。

I would say LINQ to XML is primarily traversal strategy which could also be done for the purpose of deserialization. 我会说LINQ to XML主要是遍历策略,也可以用于反序列化。

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

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