user3620627
user3620627

Reputation: 39

WCF pass XML document

I created WCF Services. I have two files:

IHelloService.cs file:

namespace HelloService
{

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

HelloService.cs file:

public void GetXmlDocumentt(XmlDocument Doc)
{
   // return Doc;
}

I chose "Add Service Reference" and add my service reference and type in my client application:

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

I don't know why? because when I repleace type from XmlDocument to string in interface and I will pass client.GetXmlDocumentt("any data") string in this line, everything works good.

Can you explain me why string type works but XmlDocument don't work?

How can I change the code to XmlDocument work good?

Thanks

Upvotes: 0

Views: 1785

Answers (1)

Redwan
Redwan

Reputation: 758

XmlDocument doesn't implement IXmlSerializable interface, which is used by DataContractSerializer. You have 3 ways:

  1. Use Xml.Linq to deal with xml and send your data in XDocument class.
  2. Send your xml as string and validate it on client side.
  3. You can serialize your document to bytes stream and then deserialize it on client side.

Upvotes: 2

Related Questions