1

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?

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
Jaggu
  • 6,298
  • 16
  • 58
  • 96

3 Answers3

1

Well, there are a number of ways. You could use XmlReader, load it into an XmlDocument, and more.

The former of those options exposes a constructor that accepts an input stream and an XmlReaderSettings instance.

But how is this response returned? If you showed us some code, or provided a little more information then you might find we have different approaches that are more appropriate.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • You mean I need to manually parse the Xml and sacrifice all benefits of strongly types language? I want the response in more OOP way where a object has properties and variables. This way of parsing is extremely tedious and error prone. – Jaggu Jul 26 '11 at 09:07
  • I agree, but if you had strongly typed classes that correlate with this data then I thought you would have known, and therefore mentioned it. As I said, if you provide more information, perhaps some code, we might be able to aid you as desired. – Grant Thomas Jul 26 '11 at 09:09
  • I made a Restful service which returns a List. Publishers is basically a Entity. I am using EntityFramework v4.1. Does this help? Or should I provide you this code? – Jaggu Jul 26 '11 at 09:14
1

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.

MrEyes
  • 13,059
  • 10
  • 48
  • 68
  • Thanks I want to go with option 1. Can you tell me how to use Visual Studio xsd tool? Also can we really just do add service reference to a Restful service also like plain asmx and WCF service? If so, then the classes will be created by VS2010 :) so I don't need to worry. – Jaggu Jul 26 '11 at 09:17
  • The service reference solution depends on the service implementation, if it is something like "string DoSomething(...)" and the string return is XML then you won't have the classes. If the implementation is something like "PublishInfo DoSomething(...)" then it will. As for the xsd tool this is part of the VS install and can be executed via the VS command prompt, e.g. xsd /c myschema.xsd – MrEyes Jul 26 '11 at 09:37
  • Install path for the XSD tool : C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin – MrEyes Jul 26 '11 at 09:39
1

Why do you need to parse it, why don't you use the classes generated by Visual Studio when you add the reference of your service to the WPF project?

If you really want to parse it, use the Xml libraries mentioned in the other answers

jjchiw
  • 4,375
  • 1
  • 29
  • 30
  • I am using REST service. I really didn't know you can add service reference of RESTful services also like plain WCF and asmx services. Thanks a lot. – Jaggu Jul 26 '11 at 09:12