Reading and writing XML in C #

I am using the .NET 4.0 Framework. I need to write some XML to interact with the web service. Parameters passed to the web service will be entered by the end user.

My question is: what is the recommended approach for interacting with XML these days? There seems to be a new way in every version of the .NET Framework to do the same. But this does not necessarily mean that it is the best. What does the community recommend in this case?

+6
c # xml
source share
3 answers

There is no new version in each version. There are only two of them. Unfortunately, there should be two, but, of course, no more than two.

I recommend System.Xml.Linq.XDocument and its related classes. They provide a clean, useful API that allows the use of LINQ and Enumerable extension methods. Generating an XML tree in this API is easy because you can embed constructor calls and you get code that is very similar to XML itself.

One that I do not recommend is System.Xml.XmlDocument . It is old and it was designed to follow the W3C guidelines for the XML API. Therefore, it does not support any modern or C # -specific ideas, including enumerations and LINQ. Creating an XML tree in this API is tortuous: you need to create each element separately and call methods like AppendChild to turn them into a tree.

+9
source share

If the service is WCF or implements WS- * standards, you should be able to add a service link to it from your project and interact with it through the entity, rather than make all network calls and process messages manually.

+2
source share

Every time I need to work with XML, I think for serialization. Just mark the [Serializible] class and properties with the corresponding attribute from System.Xml.Serialization and just serialize the objects. This only works if the XML format is defined, for example. you can describe XML using classes. If you don't like @Timwi said System.Xml.Linq.XDocument is the best choice.

0
source share

All Articles