本文介绍了当 WCF 方法返回 XmlElement 时,客户端看到 XElement 返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 WCf 方法返回一个 xmlElement.当我在客户端执行服务引用时,相同的方法返回 XElement 而不是 XmlElement.我尝试了所有方法:更新服务引用、创建新的服务引用,但都无济于事.

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.

这是我的客户:

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

我收到一个错误:无法将类型 'System.Xml.Linq.XElement' 隐式转换为 'System.Xml.XmlElement'"

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

当方法在服务器端:

 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;
     }

和界面:

    [ServiceContract]
public interface IBasicService
{

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

这是怎么回事?

推荐答案

没有错.客户端不知道使用了哪种 .NET 类型,服务引用仅通知它可以返回任何 XML.它采用 XML 并将其作为 XElement 提供.它是内置行为,我认为它是由 MS 选择的,因为它的 API 更新且更舒适.为什么需要返回XmlElement的方法?

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?

这篇关于当 WCF 方法返回 XmlElement 时,客户端看到 XElement 返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 16:14