How to retrieve data from an online API?
This is a beginner so please be kind to me :)
How can I use php API in ASP.NET? This API returns an XML document. It is also capable of returning JSON.
The result 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 example, there is a service http://someservice.com/name_query.php?pincode= that takes a pincode and returns an XML document.
Is it possible to use LINQtoXML and use it. An example using XML and one with JSON would be very helpful.
a source to share
XML vs JSON first
if you are going to use an API to do some AJAX requests (for example, request an API when a user clicks on a link / image and you, for example, want to change the color of that link, prompt the user if this is ok or not ... go for JSON because you don't need to parse XML)
if you are doing everything behind the "bushes" and you only need to present the data that is processed in the code behind, then use XML.
Simple use with WebClient object
private string GetDocument(string myPin) {
String url = String.Format("http://someservice.com/name_query.php?pincode={0}", myPin);
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0;)"); // pass as Internet Explorer 7.0
Stream data = client.OpenRead(url);
StreamReader reader = new StreamReader(data);
s = reader.ReadToEnd();
data.Close();
reader.Close();
return s;
}
at this time you have all the XML you got from the API in a string, now you need to process the XML like:
imagine the output is an XML document like:
<Hit dbId="400179221" systemId="115">
<WorksiteDbId>200105072</WorksiteDbId>
<Subscribed>false</Subscribed>
<FirstName>Klaus Holse</FirstName>
<LastName>Andersen</LastName>
<Status>Active</Status>
<MainJobTitle>CEO (Managing Director, General Manager, Owner)</MainJobTitle>
<WorksiteName>Microsoft Development Center Copenhagen ApS </WorksiteName>
<Department></Department>
<Address></Address>
<Zipcode></Zipcode>
<City></City>
<WorksitePhone></WorksitePhone>
<TypeCode>TY10</TypeCode>
<WorksiteStatus>Active</WorksiteStatus>
</Hit>
the method for processing document information looks like this:
private void processDocument(string myPin) {
String xml = GetDocument(myPin);
XmlTextReader reader = new XmlTextReader(new StringReader(xml));
XmlDocument document = new XmlDocument();
document.Load(reader);
XmlNodeList list = document.SelectNodes("/XMLNode/XMLSubNode");
foreach (XmlNode node in list) // loop through all nodes
{
foreach (XmlAttribute att in node.Attributes) // loop through all attributes
{
switch (att.Name.ToLower())
{
case "dbid": myClass.DbID = Int32.Parse(att.InnerText); break;
case "systemid": myClass.SystemID = Int32.Parse(att.InnerText); break;
default: break;
}
}
foreach (XmlNode subnode in node.ChildNodes) // loop through all subnodes
{
switch (subnode.Name.ToLower()) // check what node has what
{
case "subscribed": myClass.Subscribed = bool.Parse(subnode.InnerText); break;
case "firstname": myClass.Firstname = subnode.InnerText; break;
case "lastname": myClass.Lastname = subnode.InnerText; break;
case "status": myClass.Status = subnode.InnerText; break;
...
}
}
}
}
you will have myClass populated with all the values that were returned by the API ...
as you mentioned in the first line ... this is for newbies :) and it is a good way to understand the concept of getting and using XML data ... after you understand this part then you can easily jump to LINQ2XML :)
Hope this helps ...
added
because I only saw you have XML result, here is the processDocument method to use exact XML
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Address>
<Country>US</Country>
<City>Seattle</City>
<Result>Done</Result>
</Address>
method:
private void processDocument(string myPin) {
String xml = GetDocument(myPin);
XmlTextReader reader = new XmlTextReader(new StringReader(xml));
XmlDocument document = new XmlDocument();
document.Load(reader);
XmlNodeList list = document.SelectNodes("/Address");
foreach (XmlNode node in list) // loop through all nodes
{
foreach (XmlNode subnode in node.ChildNodes) // loop through all subnodes
{
switch (subnode.Name.ToLower()) // check what node has what
{
case "country": myClass.Country =subnode.InnerText; break;
case "city": myClass.City= subnode.InnerText; break;
case "result": myClass.Result = subnode.InnerText; break;
}
}
}
}
don't forget to check for errors, such as passing in the wrong dataset, so that you can handle the error correctly.
: -)
a source to share
You can use LINQtoXML, you can use XMLDom. What language are you going to use C # or VB.NET in?
one way to do it in LINQ to XML:
var XML = XElement.Parse(xmlSourceString);
var query = from a in XML.Descendants("addressXML")
select new
{
Country = a.Element("Country").Value,
City = a.Element("City").Value
};
a source to share