Search code examples
c#wpfwcfrestwcf-rest

How can I parse REST service response?


I have set up my REST service and it works fine. This is the response that I get from the service is shown in the figure:

enter image description here

How can I parse this type of request in my WPF application?


Solution

  • The response you have there is seemingly an XML structure that contains publisher information. As for parsing it you have a number of options however all of these require/prefer you to have the schema for the resulting XML.

    1. Use the Visual Studio XSD tool to create schema classes in your project. Once you have these you can then deserialise the XML into an object. You can then use the object within your WPF application.
    2. Use XmlDocument to load the XML then use xPath queries to extract the data you need.
    3. Use XDocument (linq to XML) to load the XML then use LINQ style queries to extract the data you need.

    Personally, I would use option 1 - It does require a schema (the other options don't technically need one) but it does give you objects which IMHO are much easier to maintain and use than xpath/linq queries.

    It is also worth mentioning that depending on how the service reference was added to the client (and how the service exposes itself) you may already have this XML class bound into the client service reference. As an example you service reference in the client may allow you to do this:

    PublisherInfo pi = myServiceClient.GetPublisherInfo();
    

    In which case all the conversion from XML to PublisherInfo is handled for you. I am guessing that the sample XML above was obtain by calling the service in a browser, therefore the conversion to PublisherInfo obviously doesn't happen as this would occur within the client code.