简体   繁体   中英

How to accept ASP.NET web API controller file for HttpClient in Xamarin Forms PCL application

I have a webapi in asp.net i have a localhost address like http://localhost:50908/api/Schools/ This gives an output in XML file which is something like this:

   <ArrayOfSchool xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MasterDetail.Models">
<School>
<SchoolCode>TDSB001</SchoolCode>
<SchoolId>1</SchoolId>
<SchoolName>Claude Watson School for Arts</SchoolName>
</School>
<School>
<SchoolCode>TDSB002</SchoolCode>
<SchoolId>2</SchoolId>
<SchoolName>Harvest Community School of Arts.</SchoolName>
</School>
<School>
<SchoolCode>TDSB003</SchoolCode>
<SchoolId>3</SchoolId>
<SchoolName>Yogin Bhatt Arts School.</SchoolName>
</School>
</ArrayOfSchool>

My code for client:

 public async Task<List<School>> GetAsync()

        {
            var httpClient = new HttpClient();
            HttpResponseMessage responseGet = await httpClient.GetAsync("http://10.115.160.142/sampleSchools.xml");
            string response = await responseGet.Content.ReadAsStringAsync();//Getting response 
            response.Replace(((char)0xFEFF).ToString(), "");
            XDocument doc = XDocument.Load("http://10.115.160.142/sampleSchools.xml");
            List<School> SchoolDetailsList = new List<School>();

            //  await DisplayAlert("XmlParsing", doc.ToString, "Ok");
            foreach (var item in doc.Descendants("School"))
            {
                School schoolItem = new School();
                schoolItem.SchoolCode = item.Element("SchoolCode").Value.ToString();
                schoolItem.SchoolId = item.Element("SchoolId").Value.ToString();
                schoolItem.SchoolName = item.Element("SchoolName").Value.ToString();
                SchoolDetailsList.Add(schoolItem);

            }
            //Binding listview with server response  
            var ResList = SchoolDetailsList;


            return ResList;
        }

The like i have given above is not working for my URI, so i made a copy of the xml file and put it on xampp server. That way it is working but that is not the proper way. How to do this operation?

您可以随时尝试将其更改为iis服务器,而不是iisexpress来运行这种代码

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM