It’s a novice question so be kind to me 🙂
How can I consume a php API in ASP.NET? This API returns an XML document. It is also capable of returning JSON.
The output is shown below
XML
<?xml version="1.0" encoding="UTF-8"?>
<Address>
<Country>US</Country>
<City>Seattle</City>
<Result>Done</Result>
</Address>
JSON
{
"CountryCode" : "US",
"City" : "Seattle",
"Result" : "Done"
}
For eg: there is a service http://someservice.com/name_query.php?pincode= which accepts pincode and returns an XML document.
Can I use LINQtoXML and consume it. Please an example of consuming with XML and one with JSON will be very helpful.
XML vs JSON first
if you are going to use the API to perform some AJAX queries (like, query the API as the user click a link/image and you, for example, want to change the color of that link, witch will tell the user that it’s ok or not… go for JSON because you no need to parse the XML)
if you are doing everything behind the “bushes” and you only need to present data that is processed in the code behind, then use XML.
Simple use, with WebClient object
at this time, you have the entire XML that you got from the API in a string, all you need now is to process the XML, for example, like:
imagine that the output is an XML document like:
the method to process the document information is something like:
you will have myClass filled up with all values that were returned by the API…
as you mention in the first line… this is for novice 🙂 and it’s a good way to you understand the concept of getting and use XML data… after you understand this part, then you will move easily to LINQ2XML 🙂
I hope this helps…
added
because I only saw now that you have the output of the XML, here is the processDocument method to use the exact XML
xml:
method:
remember to check for errors, like passing a wrong set of data so you can handle the error correctly.
🙂