views:

627

answers:

1

I have created a WCF service with one method which returns a System.Xml.XmlElement:

Interface:

using System.Xml;

...

[ServiceContract]
public interface IWCFService
{
    [OperationContract]
    XmlElement Execute(...);
}

Service:

using System.Xml;

...

public XmlElement Execute(...)
{
    XmlNode node = ...;

    return (XmlElement)node;
}

When I try to access the service deployed on my server

WCFServiceClient service = new WCFServiceClient("WSHttpBinding_IWCFService");
XmlElement node = service.Execute(...);

I get the error:

Cannot implicitly convert type 'System.Xml.Linq.XElement' to 'System.Xml.XmlElement'

Searching my service solution, I cannot see any reference to System.Xml.Linq.XElement. Is it wrong of me to expect a System.Xml.XmlElement or is VS 2010 fooling around with me?

Thanks in advance.

A: 

I hate to answer my own question, but here it goes:

As far as I can see, the System.Xml.XmlElement CAN be used as a return type for a WCF service. The DataContractSerializer used by default in WCF does support this type and thus it shouldn't be necessary to use the XmlSerializer.

However my problem has nothing to do with serializing, but thanks to Raj Kaimal for suggesting this possibility.

I followed the WCF tutorials given by Microsoft, which tells you to add the WCF service as a Service Reference when you want to use the service. This seems to be okay in VS2008, but when switching to VS2010 this approach changes the return type from System.Xml.XmlElement to the (newer?) System.Xml.Linq.XElement. To solve this, you need to add the service as a Web Reference instead.

Chau
This is not an accurate answer. Your service reference was generated by downloading the XML schema that's in the WSDL. Apparently, your schema has a few places where it accepts arbitrary XML. When a client consumes such a service, the client is likely to choose something like XmlElement or XElement as the return type.
John Saunders
@John: The answer is as accurate as I can make it - atleast at present time. I'm still in the earliest state of using WCF (and XML), and my answer seem to work for me. It is nowhere near a bullit proof answer - I know - and any edits are more than welcome. Thanks for your attention and comment :)
Chau
@Chau: here's an experiment: create a data contract class. Add a method to it. Return it from a service operation. Consume it on the client. See if the method exists on the client. Unless you use the "share types" feature, the method won't be there - only what can be passed by the WSDL.
John Saunders
@John: Thanks for your comment. I haven't got the time right now to do the experiment, but I will be required to address this problem later on - thus I will return to this in the future.
Chau