Hi Musa,
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
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 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:
<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 to process the document information is something like:
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 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:
<?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;
}
}
}
}
remember to check for errors, like passing a wrong set of data so you can handle the error correctly.
:-)