C# WCF传递XML文档

C# WCF传递XML文档,c#,wcf,C#,Wcf,我创建了WCF服务。我有两个文件: IHelloService.cs文件: namespace HelloService { [ServiceContract] public interface IHelloService { [OperationContract] void GetXmlDocumentt(XmlDocument Doc); } } HelloService.cs文件: public

我创建了WCF服务。我有两个文件:

IHelloService.cs文件:

namespace HelloService
{

    [ServiceContract]
    public interface IHelloService
    {            
        [OperationContract]
        void GetXmlDocumentt(XmlDocument Doc);
    }
}

HelloService.cs文件:

public void GetXmlDocumentt(XmlDocument Doc)
{
   // return Doc;
}
我选择了“添加服务引用”,并在我的客户端应用程序中添加我的服务引用和类型:

XmlDocument xmlDoc = new XmlDocument();
//.. load xml from file

HelloService.HelloServiceClient client = new HelloService.HelloServiceClient("BasicHttpBinding_IHelloService");

client.GetXmlDocumentt(xmlDoc); // I get error in this line
我不知道为什么?因为当我在接口中将类型从XmlDocument回复到string时,我将在这一行中传递
client.GetXmlDocumentt(“任何数据”)
string,一切正常

您能解释一下为什么字符串类型可以工作,但
XmlDocument
不能工作吗

如何将代码更改为
XmlDocument
正常工作


谢谢

XmlDocument
没有实现DataContractSerializer使用的
IXmlSerializable
接口。你有3种方法:

  • 使用
    Xml.Linq
    处理Xml并在
    XDocument
    类中发送数据
  • 将xml作为字符串发送,并在客户端验证它
  • 您可以将文档序列化为字节流,然后在客户端对其进行反序列化

  • 如果我们知道错误消息所说的内容,而不仅仅是“它不工作/It错误”等等,这会有所帮助。错误消息:与“MvcLogowanie.HelloService.HelloService Client.GetXmlDocumentt(object[])”匹配的最佳重载方法有一些无效参数参数参数1:无法从“System.Xml.XmlDocument”转换为“object[]”