I am using the .NET 4.0 Framework. I need to write some XML to interact with a web service. The parameters passed to the web service will be entered by an end-user.
My question is, what is the recommended approach for interacting with XML these days? It seems with every version of the .NET Framework there is a new way to do the same thing. But that doesn't necessarily mean that it is the best. What does the community recommend in this case?
There isn’t a new one in every version. There are only two. It is unfortunate that there should be two, but there certainly aren’t more than two.
The one I recommend is System.Xml.Linq.XDocument
and its associated classes. They provide a clean, useful API which allows you to use LINQ and the Enumerable extension methods. Generating an XML tree in this API is easy because you can nest constructor calls and you end up with code that looks very much like the XML itself.
The one I don’t recommend is System.Xml.XmlDocument
. It is old, and it was designed to follow the W3C recommendations for an XML API. Consequently, it has no support for any modern or C#-specific ideas, including enumerables and LINQ. Generating an XML tree in this API is tortuous: you have to create each element individually and call methods like AppendChild
to turn them into a tree.
If the service is WCF or implements the WS-* standards, you should be able to add a service reference to it from your project, and interact with it through an entity, rather than make all the network calls and handling the messages manually.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With