Linq to Xml VS XmlSerializer VS DataContractSerializer

后端 未结 3 1038
长发绾君心
长发绾君心 2021-02-02 12:21

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 com

相关标签:
3条回答
  • 2021-02-02 12:34

    I'll pick XmlSerializer because its the most maintainable for a custom schema (assuming you have the XSD). When you are done developing the system, test its performance in its entirety and determine whether XML serialization is causing problems. If it is, you can then replace it with something that requires more work and test it again to see if there is any gains. But if XML serialization isn't an issue, then you have maintainable code.

    The time it takes to parse a small snippet of XML data may be negligible compared to communicating with the database or external systems. On systems with large memory (16GB+) you may find the GC being a bottleneck in .NET 4 and earlier (.NET 4.5 tries to solve this), especially when you work with very large data sets and streams.

    Use AutoMapper to map objects created by XSD.EXE to your entities. This will allow the database design to change without impacting the web service.

    One thing that is great about LINQ to XML is XSD validation. However, that impacts performance.

    0 讨论(0)
  • 2021-02-02 12:37

    I agree with @Werner Strydom's answer.

    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.

    This is how I solved my problem:

    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.

    Small use of LINQ in case of 1:M relationship:

    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.

    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!

    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.

    Is there any other way?

    • 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.
    0 讨论(0)
  • 2021-02-02 12:54

    Another option is to utilize LINQ and Reflection to create a generic class to serialize your object to XML. A good example of this can be found at http://primecoder.blogspot.com/2010/09/how-to-serialize-objects-to-xml-using.html . I am not sure what your XML needs to look like at the end of the day, but if it is pretty basic this could do the trick. You would not need to make changes as your entity classes add/remove/change properties, and you could use this across all of your objects (and other projects if stored in a utility DLL).

    0 讨论(0)
提交回复
热议问题