简体   繁体   中英

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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM