Hi All, I want to send a xml file to my wcf service , how can I send it ? Is there any way with Data Contracts or Message Contracts? Please help.
The DataContract defines your C# representation of the XML that goes over the wire. You don't want to look at the raw XML - trust me..... :-)
So basically, you need a C# class which is your DataContract to describe what your XML looks like. On the client and the server, you'll be working with the C# classes - not the raw XML. If your DataContract aligns with the XML format, you will be able to deserialize your raw XML into a class instance of your DataContract class.
Alternatively, you could always just add a string field to your DataContract and package up your raw XML in there and send it across.
The last alternative you have is use the basic, untyped Message
type for the WCF contract - in that case, you need to deal with raw XML both on the client and the server side - not pretty at all, but if you really want to - it's up to you.
See the MSDN documentation on raw messages and check out Kurt Claeys' blog post on it.
Marc
While it's best to follow marc_s' advice and stick with high-level data contracts, it is occasionally necessary to send arbitrary XML. To do this, you can add an XmlElement parameter to your OperationContract.
In order to do this, you must use the XmlSerializer instead of the Data Contract Serializer. You'll need to use the [XmlSerializerFormat] attribute on your service contract.
You can do something like this:
var doc = new XmlDocument();
doc.LoadXml(xmlContent);
var message = Message.CreateMessage(MessageVersion.Soap11, "urn:someRequest", new XmlNodeReader(doc));
var factory = new ChannelFactory<IRequestChannel>("serviceHttpSoap11Endpoint");
var channel = factory.CreateChannel();
var response = channel.Request(message);
channel.Close();
So, first you load the XML file in the XmlDocument object, then you adapt the above sample to send it.
I wrote a post about that a while ago: http://www.pvle.be/2009/02/send-xmldocument-using-windows-communication-foundation/