Ashish Ashu
Ashish Ashu

Reputation: 14667

Returning XML From a WCF Service

I am creating a WCF Web Service in which one method (exposed in Service) return data in XML format as given below:

    public string QueryDirectoryEntry()
    {

        XmlDocument doc = new XmlDocument();
        doc.Load(@"c:\" + FILE_NAME);
        return doc.InnerXml;
    }

If the client call this method their service return data in XML format , I want to bind this XML in the datagridview control.

The XML data is actually contains the List<MyStruct>.

class MyStruct
{
  Name..
  ID...
}

XML:

<root>
  <MyStruct>
    <Name>abc</Name>
    <ID>1</ID>
  </MyStruct>
  <MyStruct>
    <Name>abc</Name>
    <ID>2</ID>
  </MyStruct>
</root>

I want that data should be in XML so that every application can use this data either in C# or Java.

Upvotes: 1

Views: 12232

Answers (1)

John Saunders
John Saunders

Reputation: 161821

You should never return or manipulate XML as a string. Return it as XmlElement instead:

[ServiceContract]
public interface IReturnRealXml {
    [OperationContract]
    XmlElement QueryDirectoryEntry();
}

public class ReturnRealXmlNotStrings : IReturnRealXml {

    public XmlElement QueryDirectoryEntry()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(@"c:\" + FILE_NAME);
        return doc.DocumentElement;
    }
}

Upvotes: 6

Related Questions