when WCF method returns XmlElement , the client sees XElement returned?

柔情痞子 提交于 2020-01-13 19:43:06

问题


i return an xmlElement from a WCf method. when i do a service reference in the client, the same method is returning XElement instead of XmlElement. i tried everything: updating the service reference, making a new service reference, but it doesn't help.

This is my client:

ServiceReference1.BasicServiceClient basicWCfClient = new ServiceReference1.BasicServiceClient();
        XmlElement xmlelement =  basicWCfClient.GetData(5); 
        basicWCfClient.Close();

i get an error : "Cannot implicitly convert type 'System.Xml.Linq.XElement' to 'System.Xml.XmlElement'"

when the method in the server side:

 public XmlElement GetData(int value)
    {
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml("<msg><node1>Hello</node1><node2>World</node2><request_params><mynode>More</mynode></request_params></msg>");
        XmlElement xmlElement = xmldoc.DocumentElement;
        return xmlElement;
     }

and the interface:

    [ServiceContract]
public interface IBasicService
{

    [OperationContract]       
    [WebGet(UriTemplate = "GetData?value={value}")] // Add support for HTTP GET Requests
    XmlElement GetData(int value);}

what is going on ?


回答1:


Nothing is wrong. Client doesn't know which .NET type has been used and service reference informs it only that any XML can be returned. It takes XML and provides it as XElement. It is build in behavior and I think it was chosen by MS because of newer and more comfortable API. Why do you need the method to return XmlElement?




回答2:


Go to Configure Service Reference and check Reuse types in referenced assemblies

Then check System.Xml if you want XmlElement or System.Xml.Linq if you want XElement

See also this question



来源:https://stackoverflow.com/questions/3741490/when-wcf-method-returns-xmlelement-the-client-sees-xelement-returned

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!