0

I have a third-party web service that I need to use. The calls to it will be something like this: https://somesitename.com/Service.dll?[operation]?[parameter1]&[parameter2]

It returns various XML structures depending on the operation called and the results of the operation. It can be as simple as this:

<?xml version="1.0"?>
<RESULTS>
    <VALUE>some return value here</VALUE>
</RESULTS>

Or for a call that returns slightly more complex data, it could be like this:

<?xml version="1.0" ?> 
<RESULTS>
    <RETURN>
        <RESPONSEMSG>Some message.</RESPONSEMSG> 
    </RETURN>
    <DATA>
        <COUNT>1</COUNT> 
        <VALUE1>1</VALUE1> 
        <VALUE2>1</VALUE2> 
        <VALUE3>FALSE</VALUE3> 
    </DATA>
</RESULTS>

If that call had errored instead, it would just return something like:

<?xml version="1.0" ?> 
<RESULTS>
    <RETURN>
        <ERROR>Error Message.</ERROR>
    </RETURN> 
</RESULTS>

So, basically I'm just trying to consume the XML response from a REST service. There is no data contract for it, and all I have to go on is the example responses that were provided to me. Should I build poco classes for each type of result? I'm thinking that I would use HTTPClient or WebClient to make the requests to the service. Then I'd need to somehow deserialize the resulting XML into an object that I can return. Is there a best practice or pattern for this kind of thing? Any advice would be appreciated, thanks!

ADent
  • 35
  • 1
  • 3
  • Have you tried Linq to xml? – Thinking Sites Jul 09 '14 at 17:19
  • Here's a good article on this http://support.microsoft.com/kb/307643 Also I believe this question has been answered before here http://stackoverflow.com/questions/14904171/read-xml-from-url – user1477388 Jul 09 '14 at 17:20
  • I think it is a good practice to work with object (poco) , when you receive the response deserialize it into an object to do this you can use RestSharp it is a very helpfull library. – Lamourou abdallah Jul 09 '14 at 17:59
  • I ended up loading the response into an XDocument and then checking for the existence of the elements that I was interested in. I'm still considering loading the whole thing into a POCO, but plan to come back to that once I've finished the rest of it. Thanks to everyone for the responses! – ADent Jul 14 '14 at 15:36

2 Answers2

0

lots of technologies

linq2xml or you can use xpath, there are loads of xml parsing libraries out there.

linq2xml is probably the easiest these days though.

Only problem with linq2xml is if you parse the whole thing in 1 function you really want to validate the xml structure before hand - to find the problems.

John Nicholas
  • 4,778
  • 4
  • 31
  • 50
0

Yes POCO is best for this case. Then parse the XML using LINQ-to-XML. Its always good to go for strongly-typed programming

Anup Sharma
  • 2,053
  • 20
  • 30