1

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ashish Ashu
  • 14,169
  • 37
  • 86
  • 117
  • On re-reading this question, I see that the OP may have thought he _had to_ return XML in order for a Java or other non-.NET application to use it. That is not the case. If he had just returned `List`, then WCF would have serialized it into XML for him. – John Saunders Feb 12 '13 at 17:32

1 Answers1

6

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;
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397