简体   繁体   English

VS2010将System.Xml.XmlElement与System.Xml.Linq.XElement混淆?

[英]VS2010 confuses System.Xml.XmlElement with System.Xml.Linq.XElement?

I have created a WCF service with one method which returns a System.Xml.XmlElement : 我用一个返回System.Xml.XmlElement方法创建了一个WCF服务:

Interface: 接口:

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

Service: 服务:

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' 无法将类型'System.Xml.Linq.XElement'隐式转换为'System.Xml.XmlElement'

Searching my service solution, I cannot see any reference to System.Xml.Linq.XElement . 搜索我的服务解决方案,我看不到对System.Xml.Linq.XElement任何引用。 Is it wrong of me to expect a System.Xml.XmlElement or is VS 2010 fooling around with me? 期待System.Xml.XmlElement或VS 2010和我一起System.Xml.XmlElement我是不对的?

What I have found works the best and easiest for me is to right-click on the service reference and then click on "Configure Service Reference". 我发现的最好和最简单的方法是右键单击服务引用,然后单击“配置服务引用”。 Once in the dialog box for this at the bottom you will see 2 radio buttons that say "Reuse types in all referenced assemblies" or "Reuse types in specified referenced assemblies". 进入底部的对话框后,您将看到2个单选按钮,表示“在所有引用的程序集中重用类型”或“在指定的引用程序集中重用类型”。 Click on the 2nd option for 'specified assemblies'. 单击“指定程序集”的第二个选项。 The listbox will then be enabled and you can check all assemblies and leave System.Xml.Linq unchecked . 然后将启用列表框,您可以检查所有程序集并取消选中 System.Xml.Linq This way every time you update the service reference you will get the expected results and this will be stored in the configuration of the client. 这样,每次更新服务引用时,您都将获得预期的结果,并将其存储在客户端的配置中。

1)从客户端删除system.xml.linq引用2)更新服务引用3)重新生成的Reference.cs文件现在将引用XmlElement而不是XElement

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. 据我所知, System.Xml.XmlElement可以用作WCF服务的返回类型。 The DataContractSerializer used by default in WCF does support this type and thus it shouldn't be necessary to use the XmlSerializer . 默认情况下在WCF中使用的DataContractSerializer确实支持此类型,因此不必使用XmlSerializer

However my problem has nothing to do with serializing, but thanks to Raj Kaimal for suggesting this possibility. 然而,我的问题与序列化无关,但感谢Raj Kaimal建议这种可能性。

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. 我按照Microsoft提供的WCF教程 ,告诉您在要使用该服务时将WCF服务添加为服务引用 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 . 这似乎在VS2008中没问题,但是当切换到VS2010时,这种方法将返回类型System.Xml.XmlElement更改为(较新的?) System.Xml.Linq.XElement To solve this, you need to add the service as a Web Reference instead. 要解决此问题,您需要将服务添加为Web引用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Web服务:无法将类型'System.Xml.Linq.XElement'隐式转换为'System.Xml.XmlElement' - Webservices : Cannot implicitly convert type 'System.Xml.Linq.XElement' to 'System.Xml.XmlElement' System.Xml.Linq.XElement中的查询元素 - Query elements in System.Xml.Linq.XElement System.Xml.Linq.XElement'不包含'XPathEvaluate的定义 - System.Xml.Linq.XElement' does not contain a definition for 'XPathEvaluate 扩展System.Xml.Linq.XElement-内部CloneNode - Extending System.Xml.Linq.XElement - internal CloneNode 如何使用XmlWriter将System.Xml.Linq.XElement写入流 - How to write System.Xml.Linq.XElement using XmlWriter to a stream 如何反序列化 System.Xml.Linq.XElement? - How I can deserialize a System.Xml.Linq.XElement? 将xml字符串传递给System.Xml.Linq.XElement时,避免使用尖括号将xml转义 - Avoid xml escaping of angle brackets, when passing xml string to System.Xml.Linq.XElement Linq强制转换Xelement错误:无法将类型为'System.Xml.Linq.XElement'的对象强制转换为'System.IConvertible' - Linq cast conversion Xelement error: Unable to cast object of type 'System.Xml.Linq.XElement' to type 'System.IConvertible' 对Xdocument的Linq查询返回“System.linq.Enumerable + WhereSelectEnumerableIterator'2 [system.XML.Linq.Xelement,System.String] - Linq query on Xdocument returns "System.linq.Enumerable+WhereSelectEnumerableIterator'2[system.XML.Linq.Xelement,System.String] 包含 System.Xml.XmlElement 属性的 XSD 生成类 - XSD Generated Class containing a System.Xml.XmlElement property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM