Linq到Xml VS XmlSerializer VS DataContractSerializer [英] Linq to Xml VS XmlSerializer VS DataContractSerializer

查看:91
本文介绍了Linq到Xml VS XmlSerializer VS DataContractSerializer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的web method中,我得到了一些第三方C#实体类的对象.实体类不过是DataContract.这个实体类非常复杂,具有各种类型的属性,某些属性也是集合.当然,那些链接类型也是DataContracts.

In my web method, I get an object of some third party C# entity class. The entity class is nothing but the DataContract. This entity class is quite complex and has properties of various types, some properties are collections too. Of course, those linked types are also DataContracts.

我想将该DataContract实体序列化为XML ,作为Web服务业务逻辑的一部分. 仅因为XML模式完全不同,我不能直接使用DataContractSerializer(在Web方法中接收到的对象上).因此,DataContractSerializer生成的XML不会针对该模式进行验证.

I want to serialize that DataContract entity into XML as part of business logic of my web service. I cannot use DataContractSerializer directly (on the object I receive in the web method) simply because the XML schema is altogether different. So the XML generated by DataContractSerializer will not get validated against the schema.

我无法总结实施方法.我可以想到以下实现方法:

I am not able to conclude the approach I should follow for implementation. I could think of following implementation approaches:

  1. LINQ to XML -看起来不错,但我需要为每种类型的对象手动创建XML树(即元素或类实例的XML表示形式).由于存在许多实体类,并且它们彼此链接,所以我认为手动编写XML元素的工作量很大.此外,当实体类引入一些新属性时,我将不得不继续修改XML树. 不仅如此,我生成XML树的代码看起来也不那么笨拙(至少在外观上),并且将来很难由其他一些开发人员进行维护/更改.他/她将必须仔细研究它以了解XML的生成方式.

  1. LINQ to XML - This looks ok but I need to create XML tree (i.e. elements or XML representation of the class instance) manually for each type of object. Since there are many entity classes and they are linked to each other, I think this is too much of work to write XML elements manually. Besides, i'll have to keep modifying the XML Tree as and when the entity class introduces some new property. Not only this, the code where I generate XML tree would look little clumsy (at least in appearance) and would be harder to maintain/change by some other developer in future; he/she will have to look at it so closely to understand how that XML is generated.

XmlSerializer -我可以编写自己的实体类,以表示所需的XML结构.现在,我需要将详细信息从传入对象复制到我自己的类的对象中. 这是额外的工作(代码执行时也适用于.NET!).然后,我可以在对象上使用XmlSerializer来生成XML.在这种情况下,我将必须创建实体类,并且每当修改第三方实体时,都必须在类中添加新属性. (具有XmlElement或XmlAttibute属性). 但是人们建议在此主题上使用DataContractSerializer,因此除非所有方面对我都清楚,否则我不希望最终定稿.

XmlSerializer - I can write my own entity classes that represent the XML structure I want. Now, I need to copy details from incoming object to the object of my own classes. So this is additional work (for .NET too when code executes!). Then I can use XmlSerializer on my object to generate XML. In this case, I'll have to create entity classes and whenever third party entity gets modified, I'll have to just add new property in my class. (with XmlElement or XmlAttibute attributes). But people recommend DataContractSerializer over this one and so I don't want to finalize this unless all aspects are clear to me.

DataContractSerializer -在这里,由于我无法控制第三方DataContracts,因此我必须编写自己的实体类.而且我需要将详细信息从传入对象复制到我自己的类的对象中.因此,这是额外的工作.但是,由于DataContractSerializer不支持Xml属性,因此我必须实现IXmlSerializable并在WriteXml方法中生成所需的Xml. DataContractSerializer比XmlSerializer快,但是如果第三方实体发生更改,我将不得不再次处理(在WriteXml中)更改.

DataContractSerializer - Again here, I'll have to write my own entity class since I have no control over the third party DataContracts. And I need to copy details from incoming object to the object of my own classes. So this is additional work. However, since DataContractSerializer does not support Xml attributes, I'll have to implement IXmlSerializable and generate required Xml in WriteXml method. DataContractSerializer is faster than XmlSerializer, but again I'll have to handle the changes (in WriteXml) if third party entity changes.

问题:

  • 在这种情况下,哪种方法也最好考虑性能?
  • 您能建议一些更好的方法吗?
  • DataContractSerializer是否值得考虑(因为它的性能优于XmlSerilaizer),当传入实体类可能发生更改时?
  • LINQ是否真正用于序列化?还是对查询以外的其他东西真的好吗?
  • 在这种情况下,可以优先使用XmlSerializer而不是LINQ吗?如果是,为什么?
  • Which approach is best in this scenario considering performance too?
  • Can you suggest some better approach?
  • Is DataContractSerializer worth considering (because it has better performance over XmlSerilaizer) when incoming entity class is subject to change?
  • Should LINQ be really used for serialization? Or is it really good for things other than querying?
  • Can XmlSerializer be preferred over LINQ in such cases? If yes, why?

推荐答案

我同意@Werner Strydom的回答.

I agree with @Werner Strydom's answer.

我决定使用XmlSerializer ,因为代码变得可维护并且提供了我期望的性能.最重要的是,它使我可以完全控制XML结构.

I decided to use the XmlSerializer because code becomes maintainable and it offers performance I expect. Most important is that it gives me full control over the XML structure.

这是我解决问题的方式:

我根据自己的要求创建了实体类(代表各种类型的Xml元素),并通过XmlSerializer传递了根类的实例(代表根元素的类).

I created entity classes (representing various types of Xml elements) as per my requirement and passed an instance of the root class (class representing root element) through XmlSerializer.

在1:M关系中少量使用LINQ:

无论我想在特定节点(例如Department)下多次使用相同的元素(例如Employee),还是声明类型为List<T>的属性.例如Department类中的public List<Employee> Employees.在这种情况下,XmlSerializer显然在Department节点下添加了一个名为Employees的元素(它是所有Employee元素的分组).在这种情况下,我使用LINQ(在XmlSerializer序列化.NET对象之后的 )来操纵XmlSerializer生成的XElement(即XML).使用LINQ,我只需将所有Employee节点直接放在Department节点下,然后删除Employees节点.

Wherever I wanted same element (say Employee) many times under specific node (say Department) , I declared the property of type List<T>. e.g. public List<Employee> Employees in the Department class. In such cases XmlSerializer obviously added an element called Employees (which is grouping of all Employee elements) under the Department node. In such cases, I used LINQ (after XmlSerializer serialized the .NET object) to manipulate the XElement (i.e. XML) generated by XmlSerializer. Using LINQ, I simply put all Employee nodes directly under Department node and removed the Employees node.

但是,通过组合xmlSerializerLINQ,我获得了预期的性能.

However, I got the expected performance by combination of xmlSerializer and LINQ.

缺点是,当我创建的所有类很可能是内部类时,它们都必须是公共的!

Downside is that, all classes I created had to be public when they could very well be internal!

为什么不是DataContractSerializerLINQ-to-XML?

Why not DataContractSerializer and LINQ-to-XML?

  • DataContractSerializer does not allow to use Xml attributes (unless I implement IXmlSerializable). See the types supported by DataContractSerializer.
  • LINQ-to-XML (and IXmlSerializable too) makes code clumsy while creating complex XML structure and that code would definitely make other developers scratch their heads while maintaining/changing it.

还有其他方法吗?

  • Yes. As mentioned by @Werner Strydom, you can very well generate classes using XSD.exe or tool like Xsd2Code and work directly with them if you are happy with the resulting classes.

这篇关于Linq到Xml VS XmlSerializer VS DataContractSerializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆