1

Being my first WCF project, I am getting confused and need some clarification. I want to use WCF to build a web service that accepts an XML string inside a SOAP protocol.

<ITEM_SEND xml:lang="en-US">
   <T_ID>1368</T_ID>
   <PART>8058</PART>
</ITEM_SEND>

So, I am thinking that I need to do the following for my interface:

[ServiceContract]
public interface IInvService
{
    [OperationContract]
    XmlDocument GetInventory(XmlDocument query);

}

Then for my actual method I believe I would do the following:

public XmlDocument GetInventory(XmlDocument query)
{   
    XmlDocument xmlDoc = new XmlDocument();    
    //... do stuff and return xml
    return xmlDoc;
}

Am I on the right track with using XmlDocument type for this or is there another data type I should be using for the XML string?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Kram_Koorbse
  • 442
  • 5
  • 19
  • 2
    Shouldn't WCF do this for you already? https://msdn.microsoft.com/en-us/library/hh273094(v=vs.100).aspx or https://msdn.microsoft.com/en-us/library/bb412196(v=vs.110).aspx – Thorsten Dittmar Aug 30 '16 at 14:09
  • 2
    Please refer to the below link: [Similar Question at Stack Overflow](http://stackoverflow.com/questions/9098328/how-can-i-return-xml-from-a-wcf-web-service) Happy Coding! – DevCod Aug 30 '16 at 14:14
  • Have you tested it yet? What was the result? – dbc Aug 30 '16 at 17:59
  • 2
    If the xml will be always the same schema, it's better if you create a class that has the same structure of you xml, or use a string parameter, that will be cross platform compatible. – Ricardo Pontual Aug 30 '16 at 18:46
  • 1
    I **think** your best bet is to use `XElement`. Since it [implements `IXmlSerializable`](https://msdn.microsoft.com/en-us/library/system.xml.linq.xelement(v=vs.110).aspx) it should be compatible with `DataContractSerializer`. Alternatively [switch to `XmlSerializer`](https://stackoverflow.com/questions/8951319/returning-xmldocument-from-wcf-service-not-working). – dbc Aug 30 '16 at 18:58
  • @RicardoPontual The XML will be the same each time, so am I correct in interpreting your statement as using a data contract in my interface and adding a Datamember for each node in the schema? – Kram_Koorbse Aug 30 '16 at 19:00
  • You can create a class "ITEM_SEND" that has the members "T_ID" and "PART" and use this class in your interface, like GetInventory(ITEM_SEND query). And yes, you can use DataContract/DataMember in "ITEM_SEND" class. – Ricardo Pontual Aug 30 '16 at 19:46

1 Answers1

0

It's a bit redundant to send and return an XmlDocument. WCF by default uses SOAP and the basicHttpBinding, wsHttpBinding, netTcpBinding etc. all work with SOAP. I would let the framework do the serialization for you.

Instead, I'd create a DataContract or use a strongly typed object.

As an example, your DataContract might look like the following:

[DataContract]
public class ItemSend
{
    private int _id;
    private int _part;

    [DataMember]
    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }

    [DataMember]
    public int Part
    {
        get { return _part; }
        set { _part = value; }
    }
}

Your ServiceContract might look like the following:

[ServiceContract]
public interface IInvService
{
    [OperationContract]
    ItemSend GetInventory(XmlDocument query);
}

I'm not sure what the 'query' XML is, but it could be a Data Contract as well.

Here is a tutorial on DataContracts: http://wcftutorial.net/data-contract.aspx

William Xifaras
  • 5,212
  • 2
  • 19
  • 21