WCF Service - Cannot implicitly convert type System.Xml.XmlElement to System.Xml.Linq.XElement.


  • For some reason the svcutil.exe is producing the System.Xml.XmlElement data type even though my WCF Service code defines the XML.Linq.XElement.

    Where or Why is the System.Xml.XmlElement conversion happening?


    My goal is to receive a Client side Linq.XElement data type from the WCF exposed XElement Data Type.

    Code Snippet from Service1.cs produced svcutil.exe by for Client

    public System.Xml.XmlElement GetXML(int value)
    GetXMLRequest inValue = new GetXMLRequest();
    inValue.value = value;
    GetXMLResponse retVal = ((IService1)(this)).GetXML(inValue);
    return retVal.GetXMLResult;

    Code Snippets From WCF Service

    namespace MYWCFService

    public interface IService1

    XElement GetXML(int value);


    public class CompositeType
            bool boolValue = true;
            string stringValue = "Hello ";

            public bool BoolValue
                get { return boolValue; }
                set { boolValue = value; }

            public string StringValue
                get { return stringValue; }
                set { stringValue = value; }

    public XElement GetXML(int value)
    XElement returnXML =
    new XElement("Root",
    new XElement("Element", 1),
    new XElement("Element", 2),
    new XElement("Element", 3),
    new XElement("Element", 4),
    new XElement("Element", 5)

    return returnXML;

    Thank you,

    Friday, December 31, 2010 8:15 PM


All replies


    update: see update in the post below. I'm not deleting this post since the information is accurate but missing the new piece.

    the wsdl for both XElement and XmlElement looks the same:

    <xs:element minOccurs="0" name="GetDataResult" nillable="true">
       <xs:any minOccurs="0" processContents="lax"/>

    so the client cannot distinguish between them when generating the proxy.

    it actually does not matter much since both types can be used by the client successfully (they look the same on the wire), but if it does to you then you can either :

    1. change the proxy manually (which will break when you update the proxy)

    2. stop using "add reference" and instead have the client know the contract

    Maybe it is also possible to map it directly with data contract surrogate:
    WCF Security, Interoperability And Performance Blog
    • Edited by Yaron Naveh Friday, December 31, 2010 9:51 PM
    Friday, December 31, 2010 8:30 PM
  • ignore my last comment.


    svcutil /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll"  http://localhost:7171/Service?wsdl

    (assuming you have System.Xml.Linq.dll) in that path. Xelement is used only if this assembly is referenced.
    WCF Security, Interoperability And Performance Blog
    • Marked as answer by Tomb421 Monday, January 03, 2011 1:46 PM
    Friday, December 31, 2010 9:49 PM
  • Thank you Yaron Naveh!  That was it.  Awesome and Happy New Year to all.


    Monday, January 03, 2011 1:48 PM
  • Great! It works!

    Thank you very much!

    Tuesday, October 11, 2011 2:47 PM