Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 212009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:08:49+00:00 2026-05-11T18:08:49+00:00

It’s a novice question so be kind to me :) How can I consume

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:08:49+00:00Added an answer on May 11, 2026 at 6:08 pm

    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.

    🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Does anyone know how can I replace this 2 symbol below from the string
I'm using an ASP request returning a XML file containing some latin characters. By
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.