tags:

views:

86

answers:

4

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?

+2  A: 

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.

Hugo
A: 

Check this article for reading and writing xml in c# -

http://www.codeproject.com/KB/cpp/XMLReadWrite.aspx

Sachin Shanbhag
+5  A: 

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.

Timwi
Don't forget about `XPathDocument` and the `XmlReader`/`XmlWriter` classes. And there is also the XML serializer...
0xA3
A: 

Every time when I need to work with XML I think for serizlization. Just mark the class with [Serializible] and properties with corresponding attribute from System.Xml.Serialization and just serialize the objects. This works only if the format of the XML is certain e.g. you can describe the XML with classes. If you can not like @Timwi sad System.Xml.Linq.XDocument is the best choice.

ikirachen